package com.css.fxfzfxqh.modules.buildings.probabilityEvaluation.resistEarthquakesPE.service.impl;

import com.css.fxfzfxqh.base.dict.service.DictItemService;
import com.css.fxfzfxqh.base.dict.service.DictService;
import com.css.fxfzfxqh.common.utils.UpdateUtil;
import com.css.fxfzfxqh.constants.YNEnum;
import com.css.fxfzfxqh.modules.buildingIndividual.entity.BuildingIndividualEntity;
import com.css.fxfzfxqh.modules.buildingKilometerGrid.exceptions.BizException;
import com.css.fxfzfxqh.modules.buildingKilometerGrid.service.FxqhBuildingGridDataVersionService;
import com.css.fxfzfxqh.modules.buildings.VO.CurveVo;
import com.css.fxfzfxqh.modules.buildings.calculationTasks.entity.CalculationTasks;
import com.css.fxfzfxqh.modules.buildings.calculationTasks.entity.YSXMainTable;
import com.css.fxfzfxqh.modules.buildings.calculationTasks.repository.CalculationTasksRepository;
import com.css.fxfzfxqh.modules.buildings.calculationTasks.service.CalculationTasksService;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.casualtiesDE.entity.PCC;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.casualtiesDE.vo.Grid13VO;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.util.YSXUtil;
import com.css.fxfzfxqh.modules.buildings.probabilityEvaluation.publicOperate.entity.PsHazardDataValue;
import com.css.fxfzfxqh.modules.buildings.probabilityEvaluation.publicOperate.service.DistrictService;
import com.css.fxfzfxqh.modules.buildings.probabilityEvaluation.publicOperate.service.PublicOperateService;
import com.css.fxfzfxqh.modules.buildings.probabilityEvaluation.publicOperate.vo.BasicLibraryVo;
import com.css.fxfzfxqh.modules.buildings.probabilityEvaluation.resistEarthquakesPE.entity.ResistEarthquakesPECalculation;
import com.css.fxfzfxqh.modules.buildings.probabilityEvaluation.resistEarthquakesPE.param.PETasksParam;
import com.css.fxfzfxqh.modules.buildings.probabilityEvaluation.resistEarthquakesPE.repository.ResistEarthquakesPERepository;
import com.css.fxfzfxqh.modules.buildings.probabilityEvaluation.resistEarthquakesPE.service.ResistEarthquakesPEService1;
import com.css.fxfzfxqh.modules.util.Calculation;
import com.css.fxfzfxqh.modules.util.hypergraph.service.HypergraphService;
import com.css.fxfzfxqh.modules.util.repository.PccRepository;
import com.css.fxfzfxqh.modules.vulnerabilityData.entity.BuildingIndividualVulnerability;
import com.css.fxfzfxqh.util.PlatformDateUtils;
import com.css.fxfzfxqh.util.PlatformObjectUtils;
import com.css.fxfzfxqh.web.PlatformSessionContext;
import com.css.fxfzfxqh.zcpt.sys.service.SUserService;
import com.css.fxfzfxqh.zcpt.sys.service.SysAreaService;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
 * @Description: fxfzfxqh
 * @Author: lhl
 * @CreateDate: 2023/12/8 17:03
 */
@Service
public class ResistEarthquakesPEService1Impl implements ResistEarthquakesPEService1 {
    @Resource
    private ResistEarthquakesPERepository resistEarthquakesPERepository;
    @Resource
    private SysAreaService sysAreaService;
    @Resource
    SUserService sUserService;
    @Resource
    PccRepository pccRepository;
    @Resource
    DictService dictService;
    @Resource
    DictItemService dictItemService;
    @Resource
    FxqhBuildingGridDataVersionService fxqhBuildingGridDataVersionService;
    @Resource
    private PublicOperateService publicOperateService;
    @Resource
    CalculationTasksService calculationTasksService;
    @Resource
    private CalculationTasksRepository calculationTasksRepository;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    YSXUtil ysxUtil;
    @Resource
    private DistrictService districtService;
    @Resource
    HypergraphService hypergraphService;

    @Override
    public void calculation(PETasksParam param) throws Exception {
        resistEarthquakesPERepository.deleteCalculation(param.getTaskId());

        long l = System.currentTimeMillis();
        System.out.println("概率评估-结构破坏计算开始：{}");

        //获取任务
        CalculationTasks tasks = calculationTasksRepository.findById(param.getTaskId());
        List<PCC> list = pccRepository.queryToponym(tasks.getId());
        tasks.setList(list);
        for (int j = 2; j <= 5; j++) {
            //0为烈度,1为pga  超越概率转化成发生概率
            if (tasks.getLdPga().equals("0")) {
                //发生概率地震危险性烈度
                try {
                    FSLDCalculation("pgcd" + j, tasks);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                //发生概率地震危险性PGA
                try {
                    FSPGACalculation("pgcd"+j,tasks);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        //修改计算进度
        if (tasks.getSeismicInputMode().equals("3")) {
            calculationTasksRepository.updateFSProgress(tasks.getId(), "评估", "结构破坏");
        } else {
            calculationTasksRepository.updateCYProgress(tasks.getId(), "评估", "结构破坏");
        }
        boolean bool = hypergraphService.getWorkspaceReload("fxqh_resist_earthquakes_calculation_pd");
        System.out.println("概率评估-结构破坏计算结束，共用时：{}"+(System.currentTimeMillis()-l)/1000+"s");
    }
    //发生概率地震危险性烈度计算
//    private void FSLDCalculation(String damageDegree, CalculationTasks tasks) throws Exception, BizException {
//        //根据所有省市县获取省市县geom
//        String unionGeomByCode = null;
//        if (tasks.getRadio2().equals("0")) {
//            //0为省市区县
//            //根据所有省市县获取省市县geom
//            unionGeomByCode = publicOperateService.getUnionGeomByCode(tasks.getList());
//            //得到该区域内所有的省市县。
//        } else if (tasks.getRadio2().equals("1")) {
//            //1为地图圈选
//            //写入地图圈选geom
//            unionGeomByCode = tasks.getCoordinateRange();
//        }
//        //发生概率
//        Map<String, List<PsHazardDataValue>> mapVXX = new HashMap<>();
//        //超越概率
//        Table<String, List<String>, List<BigDecimal>> stringMapMap = HashBasedTable.create();
//        if (tasks.getSeismicInputMode().equals("3")) {
//            //该省市县下面的所有危险性   根据数据级别（国家级或自定义(省市区县)）和年份来查询发生概率危险性数据
//            List<PsHazardDataValue> psHazardDataValueList = publicOperateService.getPsHazardDataValueList("1", unionGeomByCode);
//            for (PsHazardDataValue obj : psHazardDataValueList) {
//                String extends1 = obj.getKilometerGridId();
//                List<PsHazardDataValue> newList;
//                if (mapVXX.containsKey(extends1)) {
//                    newList = mapVXX.get(extends1);
//                } else {
//                    newList = new ArrayList<>();
//                }
//                newList.add(obj);
//                mapVXX.put(extends1, newList);
//            }
//        } else {
//            String type = "1";
//            if (tasks.getHouseDataFlag().equals("1")) {
//                type = "0";
//            }
//            //重新组装
//            stringMapMap = publicOperateService.transition1(unionGeomByCode, tasks.getBeyondProbabilisticVersion(), type);
//        }
//        //将危险性根据格网id合并
//        //十三个结构类型的值的list;计算用
//        List<Calculation> calculationList = new ArrayList<>();
//        if (tasks.getHouseDataFlag().equals("1")) {
//            //1为网格数据
//            Map<String, Map<String, List<YSXMainTable>>> listMapMap = new HashMap<>();
//            //烈度的公里网格
//            List<Grid13VO> gridDataList = calculationTasksRepository.getGlGwDate(tasks.getId(), null);
//            //如果有数据则循环计算
//            if (gridDataList != null) {
//                for (Grid13VO obj : gridDataList) {
//                    if (PlatformObjectUtils.isEmpty(obj.getExtends1())){
//                        continue;
//                    }
//                    //计算出概率数据
//                    //当前结构类型的概率计算值
//                    BigDecimal result = BigDecimal.ZERO;
//                    //总面积*设防占比之和
//                    BigDecimal totalArea = BigDecimal.ZERO;
//                    List<PsHazardDataValue> psHazardDataValues = new ArrayList<>();
//                    //获取发生概率
//                    if (tasks.getSeismicInputMode().equals("3")) {
//                        psHazardDataValues = mapVXX.get(obj.getExtends1());
//                    } else {
//                        //超越概率转换
//                        Map<List<String>, List<BigDecimal>> row = stringMapMap.row(obj.getExtends1());
//                        if (null == row) {
//                        } else {
//                            for (Map.Entry<List<String>, List<BigDecimal>> row1 : row.entrySet()) {
//                                for (int i = 0; i < row1.getKey().size(); i++) {
//                                    PsHazardDataValue psHazardDataValue = new PsHazardDataValue();
//                                    psHazardDataValue.setImValue(row1.getKey().get(i));
//                                    psHazardDataValue.setFImValue(row1.getValue().get(i));
//                                    psHazardDataValues.add(psHazardDataValue);
//                                }
//                            }
//                        }
//                    }
//                    if (PlatformObjectUtils.isEmpty(psHazardDataValues) || psHazardDataValues.size()==0) {
//                        continue;
//                    }
//                    if (!listMapMap.containsKey(obj.getProvince() + "_" + obj.getCity() + "_" + obj.getCounty())) {
//                        Map<String, List<YSXMainTable>> temStringListMap = ysxUtil.ysxEncapsulationNew(tasks.getId(),
//                                obj.getProvince(), obj.getCity(), obj.getCounty());
//                        listMapMap.put(obj.getProvince() + "_" + obj.getCity() + "_" + obj.getCounty(), temStringListMap);
//                    }
//                    List<BigDecimal> bigDecimals = calculationByFortification(obj, psHazardDataValues,
//                            obj.getDictionary(), damageDegree, result, totalArea, tasks, listMapMap);
//                    result = bigDecimals.get(0);
//                    totalArea = bigDecimals.get(1);
//                    //计算单个结构最终结构P(砖混)=S(砖混)*（P(VI)+P(VII)。。。+P(XII)）
//                    BigDecimal multiply = totalArea.multiply(result);
//                    Calculation calculation = new Calculation();
//                    //格网id
//                    calculation.setExtends1(obj.getExtends1());
//                    //省市县
//                    calculation.setProvince(obj.getProvince());
//                    calculation.setCity(obj.getCity());
//                    calculation.setCounty(obj.getCounty());
//                    //计算值
//                    calculation.setValue(multiply);
//                    //单个面积
//                    calculation.setArea(BigDecimal.valueOf(obj.getTotalArea()));
//                    calculation.setGeom(obj.getGeom());
//                    calculationList.add(calculation);
//                }
//            }
//            //保存
//            List<ResistEarthquakesPECalculation> conditionValue = getConditionValue(calculationList, tasks, damageDegree);
//            //入库
//            if (conditionValue.size() > 0) {
//                resistEarthquakesPERepository.batchCalculation(conditionValue);
//            }
//        } else {
//            //单体数据
//            List<BuildingIndividualEntity> all = calculationTasksRepository.getMonomerDate(tasks.getId());
//            List<ResistEarthquakesPECalculation> conditionValue = new ArrayList<>();
//            for (BuildingIndividualEntity obj : all) {
//                //根据房屋单体经纬度获取对应发生概率
//                List<PsHazardDataValue> psValue = new ArrayList<>();
//                if (tasks.getSeismicInputMode().equals("3")) {
//                    //根据房屋单体经纬度获取对应发生概率
//                    psValue = publicOperateService.getFSGLbyLonAndLat(obj.getLongitude(), obj.getLatitude(), "1");
//                } else {
//                    //根据房屋单体经纬度获取对应超越概率
//                    for (String rowKey : stringMapMap.rowKeySet()) {
//                        Boolean boo = resistEarthquakesPERepository.getSTIntersects(rowKey, obj.getLongitude(), obj.getLatitude());
//                        if (boo) {
//                            Map<List<String>, List<BigDecimal>> row = stringMapMap.row(rowKey);
//                            if (null != row) {
//                                for (Map.Entry<List<String>, List<BigDecimal>> row1 : row.entrySet()) {
//                                    for (int i = 0; i < row1.getKey().size(); i++) {
//                                        PsHazardDataValue psHazardDataValue = new PsHazardDataValue();
//                                        psHazardDataValue.setImValue(row1.getKey().get(i));
//                                        psHazardDataValue.setFImValue(row1.getValue().get(i));
//                                        psValue.add(psHazardDataValue);
//                                    }
//                                }
//                            }
//                            break;
//                        }
//                    }
//                }
//                //当前结构类型的概率计算值
//                BigDecimal result = BigDecimal.ZERO;
//                //总面积*设防占比之和
//                BigDecimal totalArea = BigDecimal.ZERO;
//                //将房屋单体转换成13VO
//                Grid13VO grid13VO = get13VO(obj);
//                //计算并将值赋给result,totalArea;
//                List<BigDecimal> bigDecimals = calculationByFortification(grid13VO, psValue, obj.getStructureType1(), obj.getStructureType2(), damageDegree, result, totalArea, tasks);
//                result = bigDecimals.get(0);
//                totalArea = bigDecimals.get(1);
//                //计算P(砖混)=S(砖混)*（P(VI)+P(VII)。。。+P(XII)）/S
//                BigDecimal p = totalArea.multiply(result).divide(totalArea,6,RoundingMode.DOWN);
//                ResistEarthquakesPECalculation pECalculation = new ResistEarthquakesPECalculation();
//                //评估记录id
//                pECalculation.setAssessId(tasks.getId());
//                pECalculation.setAssessCode(tasks.getAssessCode());
//                //省市县
//                pECalculation.setProvinceName(obj.getProvince());
//                pECalculation.setCityName(obj.getCity());
//                pECalculation.setCountyName(obj.getCounty());
//
//                pECalculation.setGeographyId(obj.getId());
//                //破坏状态
//                pECalculation.setDamageDegree(damageDegree);
//                //计算值
//                pECalculation.setCalculatedValueP1(p);
//                //修正值
//                pECalculation.setCorrectionValueP1(p);
//                //写入面积
//                pECalculation.setArea(totalArea);
//                pECalculation.setCreateUser(PlatformSessionContext.getUserID());
//                pECalculation.setDelFlag(YNEnum.N.toString());
//                for (String year : tasks.getYear().split(",")) {
//                    ResistEarthquakesPECalculation obj1 = new ResistEarthquakesPECalculation();
//                    UpdateUtil.copyNullProperties(obj, obj1);
//                    //主键
//                    obj1.setId(UUID.randomUUID().toString().replaceAll("-", ""));
//                    //概率值p (T)
//                    obj1.setPt(p.multiply(new BigDecimal(year)));
//                    //T年
//                    obj1.setYears(year);
//                    obj1.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
//                    conditionValue.add(obj1);
//                }
//            }
//            //入库
//            if (conditionValue.size() > 0) {
//                resistEarthquakesPERepository.batchCalculation(conditionValue);
//            }
//        }
//    }
    //发生概率地震危险性烈度计算
    private void FSLDCalculation(String damageDegree, CalculationTasks tasks) throws Exception, BizException {
        //根据所有省市县获取省市县geom
        String unionGeomByCode = null;
        if (tasks.getRadio2().equals("0")) {
            //0为省市区县
            //根据所有省市县获取省市县geom
            unionGeomByCode = publicOperateService.getUnionGeomByCode(tasks.getList());
            //得到该区域内所有的省市县。
        } else if (tasks.getRadio2().equals("1")) {
            //1为地图圈选
            //写入地图圈选geom
            unionGeomByCode = tasks.getCoordinateRange();
        }
        //发生概率
        Map<String, List<PsHazardDataValue>> mapVXX = new HashMap<>();
        //超越概率
        Table<String, List<String>, List<BigDecimal>> stringMapMap = HashBasedTable.create();
        if (tasks.getSeismicInputMode().equals("3")) {
            //该省市县下面的所有危险性   根据数据级别（国家级或自定义(省市区县)）和年份来查询发生概率危险性数据
            List<PsHazardDataValue> psHazardDataValueList = publicOperateService.getPsHazardDataValueList1("1", unionGeomByCode,tasks);
            for (PsHazardDataValue obj : psHazardDataValueList) {
                String extends1 = obj.getId();
                List<PsHazardDataValue> newList;
                if (mapVXX.containsKey(extends1)) {
                    newList = mapVXX.get(extends1);
                } else {
                    newList = new ArrayList<>();
                }
                newList.add(obj);
                mapVXX.put(extends1, newList);
            }
        } else {
            String type = "1";
            if (tasks.getHouseDataFlag().equals("1")) {
                type = "0";
            }
            //重新组装
            stringMapMap = publicOperateService.transition1(unionGeomByCode, tasks.getBeyondProbabilisticVersion(), type);
        }
        //将危险性根据格网id合并
        //十三个结构类型的值的list;计算用
        List<Calculation> calculationList = new ArrayList<>();
        if (tasks.getHouseDataFlag().equals("1")) {
            //1为网格数据
            Map<String, Map<String, List<YSXMainTable>>> listMapMap = new HashMap<>();
            //烈度的公里网格
            List<Grid13VO> gridDataList = calculationTasksRepository.getGlGwDate(tasks.getId(), null);
            //如果有数据则循环计算
            if (gridDataList != null) {
                for (Grid13VO obj : gridDataList) {
                    if (PlatformObjectUtils.isEmpty(obj.getExtends1())){
                        continue;
                    }
                    //计算出概率数据
                    //当前结构类型的概率计算值
                    BigDecimal result = BigDecimal.ZERO;
                    //总面积*设防占比之和
                    BigDecimal totalArea = BigDecimal.ZERO;
                    List<PsHazardDataValue> psHazardDataValues = new ArrayList<>();
                    //获取发生概率
                    if (tasks.getSeismicInputMode().equals("3")) {
                        psHazardDataValues = mapVXX.get(obj.getExtends1());
                    } else {
                        //超越概率转换
                        Map<List<String>, List<BigDecimal>> row = stringMapMap.row(obj.getExtends1());
                        if (null == row) {
                        } else {
                            for (Map.Entry<List<String>, List<BigDecimal>> row1 : row.entrySet()) {
                                for (int i = 0; i < row1.getKey().size(); i++) {
                                    PsHazardDataValue psHazardDataValue = new PsHazardDataValue();
                                    psHazardDataValue.setImValue(row1.getKey().get(i));
                                    psHazardDataValue.setFImValue(row1.getValue().get(i));
                                    psHazardDataValues.add(psHazardDataValue);
                                }
                            }
                        }
                    }
                    if (PlatformObjectUtils.isEmpty(psHazardDataValues) || psHazardDataValues.size()==0) {
                        continue;
                    }
                    if (!listMapMap.containsKey(obj.getProvince() + "_" + obj.getCity() + "_" + obj.getCounty())) {
                        Map<String, List<YSXMainTable>> temStringListMap = ysxUtil.ysxEncapsulationNew(tasks.getId(),
                                obj.getProvince(), obj.getCity(), obj.getCounty());
                        listMapMap.put(obj.getProvince() + "_" + obj.getCity() + "_" + obj.getCounty(), temStringListMap);
                    }
                    List<BigDecimal> bigDecimals = calculationByFortification(obj, psHazardDataValues,
                            obj.getDictionary(), damageDegree, result, totalArea, tasks, listMapMap);
                    result = bigDecimals.get(0);
                    totalArea = bigDecimals.get(1);
                    //计算单个结构最终结构P(砖混)=S(砖混)*（P(VI)+P(VII)。。。+P(XII)）
                    BigDecimal multiply = totalArea.multiply(result);
                    Calculation calculation = new Calculation();
                    //格网id
                    calculation.setExtends1(obj.getExtends1());
                    //省市县
                    calculation.setProvince(obj.getProvince());
                    calculation.setCity(obj.getCity());
                    calculation.setCounty(obj.getCounty());
                    //计算值
                    calculation.setValue(multiply);
                    //单个面积
                    calculation.setArea(BigDecimal.valueOf(obj.getTotalArea()));
                    calculation.setGeom(obj.getGeom());
                    calculationList.add(calculation);
                }
            }
            //保存
            List<ResistEarthquakesPECalculation> conditionValue = getConditionValue(calculationList, tasks, damageDegree);
            //入库
            if (conditionValue.size() > 0) {
                resistEarthquakesPERepository.batchCalculation(conditionValue);
            }
        } else {
            //单体数据
            List<BuildingIndividualEntity> all = calculationTasksRepository.getMonomerDate(tasks.getId());
            List<ResistEarthquakesPECalculation> conditionValue = new ArrayList<>();
            for (BuildingIndividualEntity obj : all) {
                //根据房屋单体经纬度获取对应发生概率
                List<PsHazardDataValue> psValue = new ArrayList<>();
                if (tasks.getSeismicInputMode().equals("3")) {
                    //根据房屋单体经纬度获取对应发生概率
                    psValue = publicOperateService.getFSGLbyLonAndLat(obj.getLongitude(), obj.getLatitude(), "1");
                } else {
                    //根据房屋单体经纬度获取对应超越概率
                    for (String rowKey : stringMapMap.rowKeySet()) {
                        Boolean boo = resistEarthquakesPERepository.getSTIntersects(rowKey, obj.getLongitude(), obj.getLatitude());
                        if (boo) {
                            Map<List<String>, List<BigDecimal>> row = stringMapMap.row(rowKey);
                            if (null != row) {
                                for (Map.Entry<List<String>, List<BigDecimal>> row1 : row.entrySet()) {
                                    for (int i = 0; i < row1.getKey().size(); i++) {
                                        PsHazardDataValue psHazardDataValue = new PsHazardDataValue();
                                        psHazardDataValue.setImValue(row1.getKey().get(i));
                                        psHazardDataValue.setFImValue(row1.getValue().get(i));
                                        psValue.add(psHazardDataValue);
                                    }
                                }
                            }
                            break;
                        }
                    }
                }
                //当前结构类型的概率计算值
                BigDecimal result = BigDecimal.ZERO;
                //总面积*设防占比之和
                BigDecimal totalArea = BigDecimal.ZERO;
                //将房屋单体转换成13VO
                Grid13VO grid13VO = get13VO(obj);
                //计算并将值赋给result,totalArea;
                List<BigDecimal> bigDecimals = calculationByFortification(grid13VO, psValue, obj.getStructureType1(), obj.getStructureType2(), damageDegree, result, totalArea, tasks);
                result = bigDecimals.get(0);
                totalArea = bigDecimals.get(1);
                //计算P(砖混)=S(砖混)*（P(VI)+P(VII)。。。+P(XII)）/S
                BigDecimal p = totalArea.multiply(result).divide(totalArea,6,RoundingMode.DOWN);
                ResistEarthquakesPECalculation pECalculation = new ResistEarthquakesPECalculation();
                //评估记录id
                pECalculation.setAssessId(tasks.getId());
                pECalculation.setAssessCode(tasks.getAssessCode());
                //省市县
                pECalculation.setProvinceName(obj.getProvince());
                pECalculation.setCityName(obj.getCity());
                pECalculation.setCountyName(obj.getCounty());

                pECalculation.setGeographyId(obj.getId());
                //破坏状态
                pECalculation.setDamageDegree(damageDegree);
                //计算值
                pECalculation.setCalculatedValueP1(p);
                //修正值
                pECalculation.setCorrectionValueP1(p);
                //写入面积
                pECalculation.setArea(totalArea);
                pECalculation.setCreateUser(PlatformSessionContext.getUserID());
                pECalculation.setDelFlag(YNEnum.N.toString());
                for (String year : tasks.getYear().split(",")) {
                    ResistEarthquakesPECalculation obj1 = new ResistEarthquakesPECalculation();
                    UpdateUtil.copyNullProperties(obj, obj1);
                    //主键
                    obj1.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                    //概率值p (T)
                    obj1.setPt(p.multiply(new BigDecimal(year)));
                    //T年
                    obj1.setYears(year);
                    obj1.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
                    conditionValue.add(obj1);
                }
            }
            //入库
            if (conditionValue.size() > 0) {
                resistEarthquakesPERepository.batchCalculation(conditionValue);
            }
        }
    }

    //根据设防烈度计算
//    private List<BigDecimal> calculationByFortification(Grid13VO obj, List<PsHazardDataValue> psHazardDataValues,
//                                                        String dictItemCode, String damageDegree, BigDecimal result,
//                                                        BigDecimal totalArea, CalculationTasks tasks, Map<String, Map<String, List<YSXMainTable>>> listMapMap) {
//        ArrayList<BigDecimal> list = new ArrayList<>();
//        //计算 计算的时候传入易损性数据
//        Map<String, List<YSXMainTable>> listMap = listMapMap.get(obj.getProvince() + "_" + obj.getCity() + "_" + obj.getCounty());
//        //所有烈度
//        for (PsHazardDataValue psHazardDataValue : psHazardDataValues) {
//            if (PlatformObjectUtils.isEmpty(listMap)) {
//                continue;
//            }
//            String structureTypeName = YSXUtil.getStructureType(dictItemCode);
//            //烈度转换
//            String getImValue = convert2(psHazardDataValue.getImValue());
//            String imValue = convert(getImValue);
//            String conditionName = ysxUtil.ldConversion(imValue);
//            String phcd = ysxUtil.phcdConversion(damageDegree);
//            List<YSXMainTable> ysxMainTables = listMap.get(structureTypeName + phcd + conditionName);
//            if (PlatformObjectUtils.isEmpty(ysxMainTables)) {
//                continue;
//            }
//            Map<String, Double> ysxData = new HashMap<>();
//            for (YSXMainTable entity : ysxMainTables) {
//                ysxData.put(entity.getFortify(), Double.valueOf(entity.getRate().replace("%", "")));
//            }
//            //无易损性数据则则跳过计算
//            if (ysxData.size() == 0) {
//                continue;
//            }
//            //设防烈度为0的标记,如果为5则说明所有设防烈度都为0
//            int fang = 0;
//            //计算出各个设防的值
//            //如果不设防不等于0
//            if (obj.getSfIntensity() >= 0.0009) {
//                //计算当前设防状态的面积
//                //获取当前设防状态占比
//                BigDecimal bigDecimal = BigDecimal.valueOf(obj.getSfIntensity()).divide(BigDecimal.valueOf(100),8, BigDecimal.ROUND_DOWN);
//                BigDecimal multiply = BigDecimal.valueOf(obj.getTotalArea()).multiply(bigDecimal);
//                //根据条件获取易损性值所有烈度的
//                BigDecimal gl2 = GLCalculate(ysxData.get("未设防"), psHazardDataValue.getFImValue());
//                //所有防震等级都相加结果相加
//                result = result.add(gl2);
//                //面积
//                totalArea = totalArea.add(multiply);
//            } else {
//                fang = fang + 1;
//            }
//            //如果现设防烈度_6度房屋面积占比不等于0
//            if (obj.getSfIntensity6() >= 0.0009) {
//                BigDecimal bigDecimal = BigDecimal.valueOf(obj.getSfIntensity6()).divide(BigDecimal.valueOf(100),8, BigDecimal.ROUND_DOWN);
//                BigDecimal multiply = BigDecimal.valueOf(obj.getTotalArea()).multiply(bigDecimal);
//                BigDecimal gl2 = GLCalculate(ysxData.get("未设防"), psHazardDataValue.getFImValue());
//                //所有防震等级都相加结果相加
//                result = result.add(gl2);
//                totalArea = totalArea.add(multiply);
//            } else {
//                fang = fang + 1;
//            }
//            //如果现设防烈度_7度房屋面积占比不等于0
//            if (obj.getSfIntensity7() >= 0.0009) {
//                BigDecimal bigDecimal = BigDecimal.valueOf(obj.getSfIntensity7()).divide(BigDecimal.valueOf(100),8, BigDecimal.ROUND_DOWN);
//                BigDecimal multiply = BigDecimal.valueOf(obj.getTotalArea()).multiply(bigDecimal);
//                BigDecimal gl2 = GLCalculate(ysxData.get("未设防"), psHazardDataValue.getFImValue());
//                //所有防震等级都相加结果相加
//                result = result.add(gl2);
//                totalArea = totalArea.add(multiply);
//            } else {
//                fang = fang + 1;
//            }
//            //如果现设防烈度_8度房屋面积占比不等于0
//            if (obj.getSfIntensity8() >= 0.0009) {
//                BigDecimal bigDecimal = BigDecimal.valueOf(obj.getSfIntensity8()).divide(BigDecimal.valueOf(100),8, BigDecimal.ROUND_DOWN);
//                BigDecimal multiply = BigDecimal.valueOf(obj.getTotalArea()).multiply(bigDecimal);
//                BigDecimal gl2 = GLCalculate(ysxData.get("未设防"), psHazardDataValue.getFImValue());
//                //所有防震等级都相加结果相加
//                result = result.add(gl2);
//                totalArea = totalArea.add(multiply);
//            } else {
//                fang = fang + 1;
//            }
//            //如果现设防烈度_9度房屋面积占比不等于0
//            if (obj.getSfIntensity9() >= 0.0009) {
//                BigDecimal bigDecimal = BigDecimal.valueOf(obj.getSfIntensity9()).divide(BigDecimal.valueOf(100),8, BigDecimal.ROUND_DOWN);
//                BigDecimal multiply = BigDecimal.valueOf(obj.getTotalArea()).multiply(bigDecimal);
//                BigDecimal gl2 = GLCalculate(ysxData.get("未设防"), psHazardDataValue.getFImValue());
//                //所有防震等级都相加结果相加
//                result = result.add(gl2);
//                totalArea = totalArea.add(multiply);
//            } else {
//                fang = fang + 1;
//            }
//            //如果没有一个设防结构有值则默认为未设防100%,并且在有总面积的时候计算
//            if (fang == 5 & obj.getTotalArea() >= 0.01) {
//                BigDecimal bigDecimal = BigDecimal.valueOf(obj.getTotalArea());
//                //根据条件获取易损性值
//                BigDecimal gl2 = GLCalculate(ysxData.get("未设防"), psHazardDataValue.getFImValue());
//                //所有防震等级都相加结果相加
//                result = result.add(gl2);
//                totalArea = totalArea.add(bigDecimal);
//            }
//        }
//        list.add(result);
//        list.add(totalArea);
//        return list;
//    }
    private List<BigDecimal> calculationByFortification(Grid13VO obj, List<PsHazardDataValue> psHazardDataValues,
                                                        String dictItemCode, String damageDegree, BigDecimal result,
                                                        BigDecimal totalArea, CalculationTasks tasks, Map<String, Map<String, List<YSXMainTable>>> listMapMap) {
        ArrayList<BigDecimal> list = new ArrayList<>();
        //计算 计算的时候传入易损性数据
        Map<String, List<YSXMainTable>> listMap = listMapMap.get(obj.getProvince() + "_" + obj.getCity() + "_" + obj.getCounty());
        //所有烈度
        for (PsHazardDataValue psHazardDataValue : psHazardDataValues) {
            if (PlatformObjectUtils.isEmpty(listMap)) {
                continue;
            }
            //结构类型
            String structureTypeName = YSXUtil.getStructureType(dictItemCode);
            //烈度转换
            String getImValue = convert3(psHazardDataValue.getImValue());
            String imValue = convert(getImValue);
            //烈度
            String conditionName = ysxUtil.ldConversion(imValue);
            //破坏程度
            String phcd = ysxUtil.phcdConversion(damageDegree);
            //易损性
            List<YSXMainTable> ysxMainTables = listMap.get(structureTypeName + phcd + conditionName);
            if (PlatformObjectUtils.isEmpty(ysxMainTables)) {
                continue;
            }
            Map<String, Double> ysxData = new HashMap<>();
            for (YSXMainTable entity : ysxMainTables) {
                ysxData.put(entity.getFortify(), Double.valueOf(entity.getRate().replace("%", "")));
            }
            //无易损性数据则则跳过计算
            if (ysxData.size() == 0) {
                continue;
            }
            //设防烈度为0的标记,如果为5则说明所有设防烈度都为0
            int fang = 0;
            //计算出各个设防的值
            //如果不设防不等于0
            if (obj.getSfIntensity() >= 0.0009) {
                //计算当前设防状态的面积
                //获取当前设防状态占比
                BigDecimal bigDecimal = BigDecimal.valueOf(obj.getSfIntensity()).divide(BigDecimal.valueOf(100),8, BigDecimal.ROUND_DOWN);
                //设防烈度面积
                BigDecimal multiply = BigDecimal.valueOf(obj.getTotalArea()).multiply(bigDecimal);
                //根据条件获取易损性值所有烈度的
                BigDecimal gl2 = GLCalculate(ysxData.get("未设防"), psHazardDataValue.getFImValue());
                //所有防震等级都相加结果相加
                result = result.add(gl2);
                //面积
                totalArea = totalArea.add(multiply);
            } else {
                fang = fang + 1;
            }
            //如果现设防烈度_6度房屋面积占比不等于0
            if (obj.getSfIntensity6() >= 0.0009) {
                BigDecimal bigDecimal = BigDecimal.valueOf(obj.getSfIntensity6()).divide(BigDecimal.valueOf(100),8, BigDecimal.ROUND_DOWN);
                BigDecimal multiply = BigDecimal.valueOf(obj.getTotalArea()).multiply(bigDecimal);
                BigDecimal gl2 = GLCalculate(ysxData.get("6度"), psHazardDataValue.getFImValue());
                //所有防震等级都相加结果相加
                result = result.add(gl2);
                totalArea = totalArea.add(multiply);
            } else {
                fang = fang + 1;
            }
            //如果现设防烈度_7度房屋面积占比不等于0
            if (obj.getSfIntensity7() >= 0.0009) {
                BigDecimal bigDecimal = BigDecimal.valueOf(obj.getSfIntensity7()).divide(BigDecimal.valueOf(100),8, BigDecimal.ROUND_DOWN);
                BigDecimal multiply = BigDecimal.valueOf(obj.getTotalArea()).multiply(bigDecimal);
                BigDecimal gl2 = GLCalculate(ysxData.get("7度"), psHazardDataValue.getFImValue());
                //所有防震等级都相加结果相加
                result = result.add(gl2);
                totalArea = totalArea.add(multiply);
            } else {
                fang = fang + 1;
            }
            //如果现设防烈度_8度房屋面积占比不等于0
            if (obj.getSfIntensity8() >= 0.0009) {
                BigDecimal bigDecimal = BigDecimal.valueOf(obj.getSfIntensity8()).divide(BigDecimal.valueOf(100),8, BigDecimal.ROUND_DOWN);
                BigDecimal multiply = BigDecimal.valueOf(obj.getTotalArea()).multiply(bigDecimal);
                BigDecimal gl2 = GLCalculate(ysxData.get("8度"), psHazardDataValue.getFImValue());
                //所有防震等级都相加结果相加
                result = result.add(gl2);
                totalArea = totalArea.add(multiply);
            } else {
                fang = fang + 1;
            }
            //如果现设防烈度_9度房屋面积占比不等于0
            if (obj.getSfIntensity9() >= 0.0009) {
                BigDecimal bigDecimal = BigDecimal.valueOf(obj.getSfIntensity9()).divide(BigDecimal.valueOf(100),8, BigDecimal.ROUND_DOWN);
                BigDecimal multiply = BigDecimal.valueOf(obj.getTotalArea()).multiply(bigDecimal);
                BigDecimal gl2 = GLCalculate(ysxData.get("9度"), psHazardDataValue.getFImValue());
                //所有防震等级都相加结果相加
                result = result.add(gl2);
                totalArea = totalArea.add(multiply);
            } else {
                fang = fang + 1;
            }
            //如果没有一个设防结构有值则默认为未设防100%,并且在有总面积的时候计算
            if (fang == 5 & obj.getTotalArea() >= 0.01) {
                BigDecimal bigDecimal = BigDecimal.valueOf(obj.getTotalArea());
                //根据条件获取易损性值
                BigDecimal gl2 = GLCalculate(ysxData.get("未设防"), psHazardDataValue.getFImValue());
                //所有防震等级都相加结果相加
                result = result.add(gl2);
                totalArea = totalArea.add(bigDecimal);
            }
        }
        list.add(result);
        list.add(totalArea);
        return list;
    }
    private String convert2(String imValue) {
        if (null == imValue) {
            return null;
        }
        switch (imValue) {
            case "Ⅴ":
                return "V";
            case "Ⅵ":
                return "VI";
            case "Ⅶ":
                return "VII";
            case "Ⅷ":
                return "VIII";
            case "Ⅸ":
                return "IX";
            case "Ⅹ":
                return "X";
            case "Ⅺ":
                return "XI";
            case "Ⅻ":
                return "XII";
        }
        return null;
    }

    private String convert3(String imValue) {
        if (null == imValue) {
            return null;
        }
        switch (imValue) {
            case "0.04g":
                return "VI";
            case "0.09g":
                return "VII";
            case "0.19g":
                return "VIII";
            case "0.38g":
                return "IX";
            case "0.75g":
                return "X";
        }
        return null;
    }
    //将超越概率的烈度转换成字典值
    private String convert(String imValue) {
        if (null == imValue) {
            return null;
        }
        switch (imValue) {
            case "V":
                return "ld1";
            case "VI":
                return "ld1";
            case "VII":
                return "ld2";
            case "VIII":
                return "ld3";
            case "IX":
                return "ld4";
            case "X":
                return "ld5";
            case "XI":
                return "ld5";
            case "XII":
                return "ld5";
        }
        return null;
    }

    //概率计算
    private BigDecimal GLCalculate(Double ysxValue, BigDecimal fImValue) {
        BigDecimal ysxnum = BigDecimal.valueOf(ysxValue).divide(BigDecimal.valueOf(100),8, BigDecimal.ROUND_DOWN);
        //计算概率 -ln(1-发生概率)*易损性
        BigDecimal x = BigDecimal.valueOf(1).subtract(fImValue);
        //概率
        BigDecimal gl = BigDecimal.valueOf(-Math.log(x.doubleValue())).setScale(6, RoundingMode.DOWN);
        //概率
        BigDecimal gl2 = gl.multiply(ysxnum);
        return gl2;
    }

    //概率计算
    private BigDecimal GLCalculate_new(Double ysxValue, BigDecimal fImValue) {
        BigDecimal ysxnum = BigDecimal.valueOf(ysxValue).divide(BigDecimal.valueOf(100),8, BigDecimal.ROUND_DOWN);
        //计算概率 -ln(1-发生概率)*易损性
        BigDecimal x = BigDecimal.valueOf(1).subtract(fImValue);
        //概率
        BigDecimal gl = BigDecimal.valueOf(-Math.log(x.doubleValue())).setScale(6, RoundingMode.DOWN);
        //概率
        BigDecimal gl2 = gl.multiply(ysxnum);
        return gl2;
    }


    //将房屋单体转换成Grid13VO
    private Grid13VO get13VO(BuildingIndividualEntity obj) {
        Grid13VO grid13VO = new Grid13VO();
        //写入面积
        grid13VO.setTotalArea(Double.valueOf(obj.getArea()));
        //写入设防占比
        grid13VO.setSfIntensity(0.00);
        grid13VO.setSfIntensity6(0.00);
        grid13VO.setSfIntensity7(0.00);
        grid13VO.setSfIntensity8(0.00);
        grid13VO.setSfIntensity9(0.00);
        grid13VO.setStructureType1(obj.getStructureType1());
        grid13VO.setStructureType2(obj.getStructureType2());
        if ("0".equals(obj.getDefenceLevel())) {
            grid13VO.setSfIntensity(100.00);
        } else if ("6".equals(obj.getDefenceLevel())) {
            grid13VO.setSfIntensity6(100.00);
        } else if ("7".equals(obj.getDefenceLevel())) {
            grid13VO.setSfIntensity7(100.00);
        } else if ("8".equals(obj.getDefenceLevel())) {
            grid13VO.setSfIntensity8(100.00);
        } else if ("9".equals(obj.getDefenceLevel())) {
            grid13VO.setSfIntensity9(100.00);
        }
        grid13VO.setBuildingCode(obj.getBuildingCode());
        return grid13VO;
    }


    private List<BigDecimal> calculationByFortification(Grid13VO obj, List<PsHazardDataValue> psHazardDataValues,
                                                        String type1, String type2, String damageDegree, BigDecimal result,
                                                        BigDecimal totalArea, CalculationTasks tasks) {
        List<BigDecimal> list = new ArrayList<>();

        //所有烈度
        for (PsHazardDataValue psHazardDataValue : psHazardDataValues) {
            //烈度转换
            String getImValue = convert3(psHazardDataValue.getImValue());
            String imValue = convert(getImValue);
            //获取易损性
            Map<String, Double> ysxData = ysxUtil.getYSX(type1, type2, damageDegree, imValue,
                    obj.getProvince(), obj.getCity(), obj.getCounty(), tasks.getId());
            //设防烈度为0的标记,如果为5则说明所有设防烈度都为0
            int fang = 0;
            //计算出各个设防的值
            //如果不设防不等于0
            if (obj.getSfIntensity() >= 0.0009) {
                //计算当前设防状态的面积
                //获取当前设防状态占比
                BigDecimal bigDecimal = BigDecimal.valueOf(obj.getSfIntensity()).divide(BigDecimal.valueOf(100),8, BigDecimal.ROUND_DOWN);
                BigDecimal multiply = BigDecimal.valueOf(obj.getTotalArea()).multiply(bigDecimal);
                //根据条件获取易损性值所有烈度的
                BigDecimal gl2 = GLCalculate(ysxData.get("未设防"), psHazardDataValue.getFImValue());
                //所有防震等级都相加结果相加
                result = result.add(gl2);
                //面积
                totalArea = totalArea.add(multiply);
            } else {
                fang = fang + 1;
            }
            //如果现设防烈度_6度房屋面积占比不等于0
            if (obj.getSfIntensity6() >= 0.0009) {
                BigDecimal bigDecimal = BigDecimal.valueOf(obj.getSfIntensity6()).divide(BigDecimal.valueOf(100),8, BigDecimal.ROUND_DOWN);
                BigDecimal multiply = BigDecimal.valueOf(obj.getTotalArea()).multiply(bigDecimal);
                BigDecimal gl2 = GLCalculate(ysxData.get("未设防"), psHazardDataValue.getFImValue());
                //所有防震等级都相加结果相加
                result = result.add(gl2);
                totalArea = totalArea.add(multiply);
            } else {
                fang = fang + 1;
            }
            //如果现设防烈度_7度房屋面积占比不等于0
            if (obj.getSfIntensity7() >= 0.0009) {
                BigDecimal bigDecimal = BigDecimal.valueOf(obj.getSfIntensity7()).divide(BigDecimal.valueOf(100),8, BigDecimal.ROUND_DOWN);
                BigDecimal multiply = BigDecimal.valueOf(obj.getTotalArea()).multiply(bigDecimal);
                BigDecimal gl2 = GLCalculate(ysxData.get("未设防"), psHazardDataValue.getFImValue());
                //所有防震等级都相加结果相加
                result = result.add(gl2);
                totalArea = totalArea.add(multiply);
            } else {
                fang = fang + 1;
            }
            //如果现设防烈度_8度房屋面积占比不等于0
            if (obj.getSfIntensity8() >= 0.0009) {
                BigDecimal bigDecimal = BigDecimal.valueOf(obj.getSfIntensity8()).divide(BigDecimal.valueOf(100),8, BigDecimal.ROUND_DOWN);
                BigDecimal multiply = BigDecimal.valueOf(obj.getTotalArea()).multiply(bigDecimal);
                BigDecimal gl2 = GLCalculate(ysxData.get("未设防"), psHazardDataValue.getFImValue());
                //所有防震等级都相加结果相加
                result = result.add(gl2);
                totalArea = totalArea.add(multiply);
            } else {
                fang = fang + 1;
            }
            //如果现设防烈度_9度房屋面积占比不等于0
            if (obj.getSfIntensity9() >= 0.0009) {
                BigDecimal bigDecimal = BigDecimal.valueOf(obj.getSfIntensity9()).divide(BigDecimal.valueOf(100),8, BigDecimal.ROUND_DOWN);
                BigDecimal multiply = BigDecimal.valueOf(obj.getTotalArea()).multiply(bigDecimal);
                BigDecimal gl2 = GLCalculate(ysxData.get("未设防"), psHazardDataValue.getFImValue());
                //所有防震等级都相加结果相加
                result = result.add(gl2);
                totalArea = totalArea.add(multiply);
            } else {
                fang = fang + 1;
            }
            //如果没有一个设防结构有值则默认为未设防100%,并且在有总面积的时候计算
            if (fang == 5 & obj.getTotalArea() >= 0.01) {
                BigDecimal bigDecimal = BigDecimal.valueOf(obj.getTotalArea());
                //根据条件获取易损性值
                BigDecimal gl2 = GLCalculate(ysxData.get("未设防"), psHazardDataValue.getFImValue());
                //所有防震等级都相加结果相加
                result = result.add(gl2);
                totalArea = totalArea.add(bigDecimal);
            }
        }
        list.add(result);
        list.add(totalArea);
        return list;
    }


    //13个计算值归并计算
    private List<ResistEarthquakesPECalculation> getConditionValue(List<Calculation> calculationList, CalculationTasks tasks
            , String damageDegree) {
        List<ResistEarthquakesPECalculation> list = new ArrayList<>();
        Map<String, List<Calculation>> map = new HashMap<>();
        //将获得的所有数据进行归并,从而获得每个一样的geom下的所有计算值,并进行计算
        for (Calculation calculation : calculationList) {
            String key = calculation.getGeom();
            List<Calculation> newList;
            if (map.containsKey(key)) {
                newList = map.get(key);
            } else {
                newList = new ArrayList<>();
            }
            newList.add(calculation);
            map.put(key, newList);
        }
        for (Map.Entry<String, List<Calculation>> entry : map.entrySet()) {
            List<Calculation> value = entry.getValue();
            BigDecimal area = BigDecimal.ZERO;
            for (Calculation calculation : value) {
                //获取格网总面积
                area = area.add(calculation.getArea());
            }
            //如果是pga则是四个破坏状态一起算
            if (tasks.getLdPga().equals("1")) {
                BigDecimal v1 = BigDecimal.ZERO;
                BigDecimal v2 = BigDecimal.ZERO;
                BigDecimal v3 = BigDecimal.ZERO;
                BigDecimal v4 = BigDecimal.ZERO;
                for (Calculation calculation : value) {
                    BigDecimal t1 = null;
                    BigDecimal t2 = null;
                    BigDecimal t3 = null;
                    BigDecimal t4 = null;
                    //S(砖混)*【0.999969*（f1(0.01）-f2(0.01))+0.961678*（f1(0.15）-f2(0.15))+。。。+0.0012*（f1(0.95）-f2(0.95))】/S
                    if (area.compareTo(BigDecimal.ZERO) == 0) {
                        t1 = BigDecimal.ZERO;
                        t2 = BigDecimal.ZERO;
                        t3 = BigDecimal.ZERO;
                        t4 = BigDecimal.ZERO;
                    } else {
//                        if (Double.parseDouble(calculation.getV1().toString()) < 0.00001) {
//                            t1 = BigDecimal.ZERO;
//                        } else {
                            t1 = calculation.getV1().divide(area, 12, BigDecimal.ROUND_DOWN).setScale(12, BigDecimal.ROUND_DOWN);
                        //}
//                        if (Double.parseDouble(calculation.getV2().toString()) < 0.00001) {
//                            t2 = BigDecimal.ZERO;
//                        } else {
                            t2 = calculation.getV2().divide(area, 12, BigDecimal.ROUND_DOWN).setScale(12, BigDecimal.ROUND_DOWN);
                        //}
//                        if (Double.parseDouble(calculation.getV3().toString()) < 0.00001) {
//                            t3 = BigDecimal.ZERO;
//                        } else {
                            t3 = calculation.getV3().divide(area, 12, BigDecimal.ROUND_DOWN).setScale(12, BigDecimal.ROUND_DOWN);
//                        }
//                        if (Double.parseDouble(calculation.getV4().toString()) < 0.00001) {
//                            t4 = BigDecimal.ZERO;
//                        } else {
                            t4 = calculation.getV4().divide(area, 12, BigDecimal.ROUND_DOWN).setScale(12, BigDecimal.ROUND_DOWN);
                        //}
                    }
                    //p(1)=P(砖混)+P(钢结构)+。。。。。
                    v1 = v1.add(t1);
                    v2 = v2.add(t2);
                    v3 = v3.add(t3);
                    v4 = v4.add(t4);
                }
                //写入list
                ArrayList<BigDecimal> list1 = new ArrayList<>();
                if("pgcd2".equals(damageDegree)){
                    list1.add(v1);
                }else if("pgcd3".equals(damageDegree)){
                    list1.add(v2);
                }else if("pgcd4".equals(damageDegree)){
                    list1.add(v3);
                }else if("pgcd5".equals(damageDegree)){
                    list1.add(v4);
                }
//                list1.add(v1);
//                list1.add(v2);
//                list1.add(v3);
//                list1.add(v4);
                for (int i = 0; i < list1.size(); i++) {
                    BigDecimal bigDecimal = list1.get(i);
                    ResistEarthquakesPECalculation obj = new ResistEarthquakesPECalculation();
                    //评估记录id
                    obj.setAssessId(tasks.getId());
                    obj.setAssessCode(tasks.getAssessCode());
                    //省市县
                    obj.setProvinceName(value.get(0).getProvince());
                    obj.setCityName(value.get(0).getCity());
                    obj.setCountyName(value.get(0).getCounty());
                    //格网id
                    obj.setExtends1(value.get(0).getExtends1());
                    obj.setGeographyId(value.get(0).getExtends1());
                    //破坏状态
                    obj.setDamageDegree(damageDegree);
                    //计算值
                    if(area.compareTo(BigDecimal.ZERO) == 0 && bigDecimal.compareTo(BigDecimal.ZERO) == 0){
                        //9999.9999/空值标记表示如果格网面积为0 那么计算值也为0 这种情况下页面不回显0 而是显示空值
                        bigDecimal = BigDecimal.valueOf(9999.9999);
                    }
                    obj.setCalculatedValueP1(bigDecimal);
                    obj.setPt(bigDecimal);
                    //修正值
                    obj.setCorrectionValueP1(bigDecimal);
                    obj.setCorrectionValuePt(bigDecimal);
                    //geom
                    obj.setGeom(value.get(0).getGeom());
                    //写入面积
                    obj.setArea(area);
                    obj.setCreateUser(PlatformSessionContext.getUserID());
                    obj.setDelFlag(YNEnum.N.toString());
                    for (String year : tasks.getYear().split(",")) {
                        ResistEarthquakesPECalculation obj1 = new ResistEarthquakesPECalculation();
                        UpdateUtil.copyNullProperties(obj, obj1);
                        //主键
                        obj1.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                        //概率值p (T)
                        obj1.setPt(bigDecimal.multiply(new BigDecimal(year)));
                        //T年
                        obj1.setYears(year);
                        obj1.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
                        list.add(obj1);
                    }
                }
            } else {
                BigDecimal p1 = BigDecimal.ZERO;
                for (Calculation calculation : value) {
                    BigDecimal divide = null;
                    //P(砖混)=S(砖混)*（P(VI)+P(VII)。。。+P(XII)））/S
                    if (area.toString().equals("0")) {
                        divide = BigDecimal.ZERO;
                    } else {
                        if (Double.parseDouble(calculation.getValue().toString()) < 0.00001) {
                            divide = BigDecimal.ZERO;
                        } else {
                            divide = calculation.getValue().divide(area, 4, BigDecimal.ROUND_DOWN).setScale(4, BigDecimal.ROUND_DOWN);
                        }
                    }
                    //p(1)=P(砖混)+P(钢结构)+。。。。。
                    p1 = p1.add(divide);
                }
                ResistEarthquakesPECalculation obj = new ResistEarthquakesPECalculation();
                //评估记录id
                obj.setAssessId(tasks.getId());
                obj.setAssessCode(tasks.getAssessCode());
                //省市县
                obj.setProvinceName(value.get(0).getProvince());
                obj.setCityName(value.get(0).getCity());
                obj.setCountyName(value.get(0).getCounty());
                //格网id
                obj.setExtends1(value.get(0).getExtends1());
                obj.setGeographyId(value.get(0).getExtends1());
                //破坏状态
                obj.setDamageDegree(damageDegree);
                //计算值
                obj.setCalculatedValueP1(p1);
                obj.setPt(p1);
                //修正值
                obj.setCorrectionValueP1(p1);
                obj.setCorrectionValuePt(p1);

                //geom
                obj.setGeom(value.get(0).getGeom());
                //写入面积
                obj.setArea(area);
                obj.setCreateUser(PlatformSessionContext.getUserID());
                obj.setDelFlag(YNEnum.N.toString());
                for (String year : tasks.getYear().split(",")) {
                    ResistEarthquakesPECalculation obj1 = new ResistEarthquakesPECalculation();
                    UpdateUtil.copyNullProperties(obj, obj1);
                    //主键
                    obj1.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                    //概率值p (T)
                    obj1.setPt(p1.multiply(new BigDecimal(year)));
                    //T年
                    obj1.setYears(year);
                    obj1.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
                    list.add(obj1);
                }
            }

        }
        return list;
    }

//    @Override
//    public void FSPGACalculation(String damageDegree, CalculationTasks tasks) throws Exception {
//        //根据所有省市县获取省市县geom
//        String unionGeomByCode = null;
//        //如果根据省市县评估则取省市县,如果根据地图圈选则默认写入一条
//        if (tasks.getRadio2().equals("0")) {
//            //0为省市区县
//            //根据所有省市县获取省市县geom
//            unionGeomByCode = publicOperateService.getUnionGeomByCode(tasks.getList());
//        } else if (tasks.getRadio2().equals("1")) {
//            //写入地图圈选geom
//            unionGeomByCode = tasks.getCoordinateRange();
//        }
//        Map<String, List<PsHazardDataValue>> mapVXX = new HashMap<>();
//        if (tasks.getSeismicInputMode().equals("3")) {
//            //该省市县下面的所有发生概率  格网
//            List<PsHazardDataValue> psHazardDataValueList = publicOperateService.getPsHazardDataValueList1("1", unionGeomByCode,tasks);
//            for (PsHazardDataValue obj : psHazardDataValueList) {
//                String extends1 = obj.getKilometerGridId();
//                List<PsHazardDataValue> newList;
//                if (mapVXX.containsKey(extends1)) {
//                    newList = mapVXX.get(extends1);
//                } else {
//                    newList = new ArrayList<>();
//                }
//                newList.add(obj);
//                mapVXX.put(extends1, newList);
//            }
//        } else {
//            String type = "1";
//            if (tasks.getHouseDataFlag().equals("1")) {
//                type = "0";
//            }
//            //重新组装 格网  超越概率
//            Table<String, List<String>, List<BigDecimal>> stringMapMap = publicOperateService.transition2(unionGeomByCode, tasks.getBeyondProbabilisticVersion(), "0");
//            for (String rowKey : stringMapMap.rowKeySet()) {
//                List<PsHazardDataValue> psHazardDataValues = new ArrayList<>();
//                //循环横行
//                for (List<String> columnKey : stringMapMap.columnKeySet()) {
//                    List<BigDecimal> bigDecimals = stringMapMap.get(rowKey, columnKey);
//                    if (null == bigDecimals) {
//                        continue;
//                    }
//                    //循环纵行
//                    for (int i = 0; i < columnKey.size(); i++) {
//                        PsHazardDataValue psHazardDataValue = new PsHazardDataValue();
//                        psHazardDataValue.setImValue(columnKey.get(i));
//                        psHazardDataValue.setFImValue(bigDecimals.get(i));
//                        psHazardDataValues.add(psHazardDataValue);
//                    }
//                }
//                mapVXX.put(rowKey, psHazardDataValues);
//            }
//        }
//        //十三个结构类型的值的list;计算用
//        List<Calculation> calculationList = new ArrayList<>();
//        if (tasks.getHouseDataFlag().equals("1")) {
//            //房屋格网
//            //第一步根据版本号查询十三个房屋类型
////            List<DictEntityVo> dictItemByDictId = dictItemService.getDictItemByDictCode("jzwglgwlx");
////            for (DictEntityVo dictEntityVo : dictItemByDictId) {
////                //烈度的公里网格
////                List<Grid13VO> gridDataList = calculationTasksRepository.getGlGwDate(tasks.getId(), dictEntityVo.getDictItemCode());
////                //如果有数据则循环计算
////                if (gridDataList != null) {
////                    for (Grid13VO obj : gridDataList) {
////                        if("32ed67423d1c4985850c60e2f7169ba6".equals(obj.getExtends1())){
////                            System.out.println("111");
////                        }
////                        if (PlatformObjectUtils.isEmpty(obj.getExtends1())){
////                            continue;
////                        }
////                        //根据省市县获取对应的PGA函数
////                        Map<String, Map<String, Map<String, BasicLibraryVo.StateValue>>> centerValue1 = publicOperateService.getCenterValue1(obj.getProvince(), obj.getCity(), obj.getCounty());
////                        //总面积*设防占比之和
////                        BigDecimal totalArea = BigDecimal.ZERO;
////                        //该结构类型下,所有设防状态 下的ysx PGA
////                        Map<String, Map<String, BasicLibraryVo.StateValue>> stringMapMap = centerValue1.get(ysxUtil.getStructureType(obj.getDictionary()));
////                        //根据geom获取对应的发生概率
////                        List<PsHazardDataValue> psHazardDataValues = mapVXX.get(obj.getExtends1());
////                        if (null == psHazardDataValues) {
////                            continue;
////                        }
////                        if (null == stringMapMap) {
////                            continue;
////                        }
////                        //计算
////                        Calculation calculation = PGAcompute(obj, stringMapMap, psHazardDataValues);
////                        calculationList.add(calculation);
////                    }
////                }
////            }
//            //烈度的公里网格
//            List<Grid13VO> gridDataListAll = calculationTasksRepository.getGlGwDate(tasks.getId(), "");
//            //如果有数据则循环计算
//            if (gridDataListAll != null) {
//                Map<String, List<Grid13VO>> collect = gridDataListAll.stream().filter(grid13VO -> "32ed67423d1c4985850c60e2f7169ba6".equals(grid13VO.getExtends1())).collect(Collectors.groupingBy(Grid13VO::getExtends1));
//                collect.forEach((k,v)->{
//                    if("32ed67423d1c4985850c60e2f7169ba6".equals(k) && damageDegree.equals("pgcd3")){
//                        System.out.println("111");
//                    }
//                    //connect的key是extends1 v是相同extends1的数据的集合
//                    List<Grid13VO> gridDataList = v;
//                    for (int i=0;i<gridDataList.size();i++) {
//                        Grid13VO obj = gridDataList.get(i);
//                        //根据省市县获取对应的PGA函数
//                        Map<String, Map<String, Map<String, BasicLibraryVo.StateValue>>> centerValue1 = null;
//                        try {
//                            centerValue1 = publicOperateService.getCenterValue1(obj.getProvince(), obj.getCity(), obj.getCounty());
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//                        //总面积*设防占比之和
//                        BigDecimal totalArea = BigDecimal.ZERO;
//                        //该结构类型下,所有设防状态 下的ysx PGA
//                        Map<String, Map<String, BasicLibraryVo.StateValue>> stringMapMap = centerValue1.get(ysxUtil.getStructureType(obj.getDictionary()));
//                        //根据geom获取对应的发生概率
//                        List<PsHazardDataValue> psHazardDataValues = mapVXX.get(obj.getExtends1());
//                        if (null == psHazardDataValues) {
//                            continue;
//                        }
//                        if (null == stringMapMap) {
//                            continue;
//                        }
//                        //计算
//                        Calculation calculation = PGAcompute(obj, stringMapMap, psHazardDataValues,damageDegree);
//                        calculationList.add(calculation);
//                    }
//                });
//
//            }
//
//            //保存
//            List<ResistEarthquakesPECalculation> conditionValue = getConditionValue(calculationList, tasks, damageDegree);
//            //入库
//            if (conditionValue.size() > 0) {
//                resistEarthquakesPERepository.batchCalculation(conditionValue);
//            }
//        } else {
//            List<ResistEarthquakesPECalculation> conditionValue = new ArrayList<>();
//            //房屋单体数据
//            List<BuildingIndividualEntity> all = calculationTasksRepository.getMonomerDate(tasks.getId());
//            for (BuildingIndividualEntity obj : all) {
//                List<PsHazardDataValue> psHazardDataValues = null;
//                if (tasks.getSeismicInputMode().equals("3")) {
//                    //根据房屋单体经纬度获取对应发生概率
//                    //psHazardDataValues = publicOperateService.getFSGLbyLonAndLat(obj.getLongitude(), obj.getLatitude(), "0");
//                    psHazardDataValues = publicOperateService.getFSGLbyLonAndLat1(obj.getLongitude(), obj.getLatitude(), "0",tasks);
//                } else {
//                    //根据房屋单体经纬度获取对应超越概率
//                    for (Map.Entry<String, List<PsHazardDataValue>> entry : mapVXX.entrySet()) {
//                        Boolean boo = resistEarthquakesPERepository.getSTIntersects(entry.getKey(), obj.getLongitude(), obj.getLatitude());
//                        if (boo) {
//                            psHazardDataValues = entry.getValue();
//                            break;
//                        }
//                    }
//                }
//                //获取结构类型
//                String type = getType(obj.getStructureType1(), obj.getStructureType2());
//                //根据省市县和房屋结构类型获取对应的PGA函数
//                Map<String, Map<String, BasicLibraryVo.StateValue>> stringMapMap = publicOperateService.getCenterValue1(obj.getProvince(), obj.getCity(), obj.getCounty()).get(type);
//                if (null == psHazardDataValues) {
//                    continue;
//                }
//                if (null == stringMapMap) {
//                    continue;
//                }
//                //将房屋单体转换成13VO
//                Grid13VO grid13VO = get13VO(obj);
//                //计算
//                Calculation calculation = PGAcompute(grid13VO, stringMapMap, psHazardDataValues,damageDegree);
//                //单体直接计算,无需13个合并 S(砖混)*【0.999969*（f1(0.01）-f2(0.01))+0.961678*（f1(0.15）-f2(0.15))+。。。+0.0012*（f1(0.95）-f2(0.95))】/S
//                BigDecimal area = calculation.getArea();
//                //获取4个计算值list
//                List<BigDecimal> pgAv4List = getPGAv4List(calculation);
//                for (int i = 0; i < pgAv4List.size(); i++) {
//                    BigDecimal bigDecimal = pgAv4List.get(i);
//                    //单体最终计算结果
//                    BigDecimal p1 = bigDecimal.divideToIntegralValue(area).setScale(6, RoundingMode.DOWN);
//                    ResistEarthquakesPECalculation pECalculation = new ResistEarthquakesPECalculation();
//                    //评估记录id
//                    pECalculation.setAssessId(tasks.getId());
//                    //评估记录code
//                    pECalculation.setAssessCode(tasks.getAssessCode());
//                    //省市县
//                    pECalculation.setProvinceName(obj.getProvince());
//                    pECalculation.setCityName(obj.getCity());
//                    pECalculation.setCountyName(obj.getCounty());
//                    //格网/单体虚拟id
//                    pECalculation.setGeographyId(obj.getId());
//                    //破坏状态
//                    pECalculation.setDamageDegree("pgcd" + (i + 1));
//                    //计算值
//                    pECalculation.setCalculatedValueP1(p1);
//                    //修正值
//                    pECalculation.setCorrectionValueP1(p1);
//                    //写入面积
//                    pECalculation.setArea(area);
//
//                    pECalculation.setCreateUser(PlatformSessionContext.getUserID());
//                    pECalculation.setDelFlag(YNEnum.N.toString());
//                    for (String year : tasks.getYear().split(",")) {
//                        ResistEarthquakesPECalculation obj1 = new ResistEarthquakesPECalculation();
//                        UpdateUtil.copyNullProperties(obj, obj1);
//                        //主键
//                        obj1.setId(UUID.randomUUID().toString().replaceAll("-", ""));
//                        //概率值p (T)
//                        obj1.setPt(bigDecimal.multiply(new BigDecimal(year)));
//                        //T年
//                        obj1.setYears(year);
//                        obj1.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
//                        conditionValue.add(obj1);
//                    }
//                }
//            }
//            //入库
//            if (conditionValue.size() > 0) {
//                resistEarthquakesPERepository.batchCalculation(conditionValue);
//            }
//        }
//    }

    @Override
    public void FSPGACalculation(String damageDegree, CalculationTasks tasks) throws Exception {
        Map<String,BigDecimal> functionMap = new HashMap<>();

        //十三个结构类型的值的list;计算用
        List<Calculation> calculationList = new ArrayList<>();
        if ("1".equals(tasks.getHouseDataFlag())) {
            //根据所有省市县获取省市县geom
            String unionGeomByCode = null;
            //如果根据省市县评估则取省市县,如果根据地图圈选则默认写入一条
            if (tasks.getRadio2().equals("0")) {
                //0为省市区县
                //根据所有省市县获取省市县geom
                unionGeomByCode = publicOperateService.getUnionGeomByCode(tasks.getList());
            } else if (tasks.getRadio2().equals("1")) {
                //写入地图圈选geom
                unionGeomByCode = tasks.getCoordinateRange();
            }
            Map<String, List<PsHazardDataValue>> mapVXX = new HashMap<>();
            if (tasks.getSeismicInputMode().equals("3")) {
                //该省市县下面的所有发生概率  格网
                List<PsHazardDataValue> psHazardDataValueList = publicOperateService.getPsHazardDataValueList1("1", unionGeomByCode,tasks);
                for (PsHazardDataValue obj : psHazardDataValueList) {
                    String extends1 = obj.getId();
                    List<PsHazardDataValue> newList;
                    if (mapVXX.containsKey(extends1)) {
                        newList = mapVXX.get(extends1);
                    } else {
                        newList = new ArrayList<>();
                    }
                    newList.add(obj);
                    mapVXX.put(extends1, newList);
                }
            } else {
                String type = "1";
                if (tasks.getHouseDataFlag().equals("1")) {
                    type = "0";
                }
                //重新组装 格网  超越概率
                Table<String, List<String>, List<BigDecimal>> stringMapMap = publicOperateService.transition2(unionGeomByCode, tasks.getBeyondProbabilisticVersion(), "0");
                for (String rowKey : stringMapMap.rowKeySet()) {
                    List<PsHazardDataValue> psHazardDataValues = new ArrayList<>();
                    //循环横行
                    for (List<String> columnKey : stringMapMap.columnKeySet()) {
                        List<BigDecimal> bigDecimals = stringMapMap.get(rowKey, columnKey);
                        if (null == bigDecimals) {
                            continue;
                        }
                        //循环纵行
                        for (int i = 0; i < columnKey.size(); i++) {
                            PsHazardDataValue psHazardDataValue = new PsHazardDataValue();
                            psHazardDataValue.setImValue(columnKey.get(i));
                            psHazardDataValue.setFImValue(bigDecimals.get(i));
                            psHazardDataValues.add(psHazardDataValue);
                        }
                    }
                    mapVXX.put(rowKey, psHazardDataValues);
                }
            }
            //房屋格网
            //烈度的公里网格
            List<Grid13VO> gridDataListAll = calculationTasksRepository.getGlGwDate(tasks.getId(), "");
            if(PlatformObjectUtils.isEmpty(gridDataListAll)){
                throw new RuntimeException("评估未完成没有获取到房屋格网数据。");
            }
            //获取格网涉及到的省市县
            List<String> districtList = gridDataListAll.stream().map(grid13VO -> grid13VO.getProvince() + "_" + grid13VO.getCity() + "_" + grid13VO.getCounty()).distinct().collect(Collectors.toList());
            //遍历获取每个曲线的易损性数据
            Map<String, Map<String, Table<String, BigDecimal,CurveVo>>> centerValue1 = new HashMap<>();
            for (String area : districtList) {
                String province = area.split("_")[0];
                String city = area.split("_")[1];
                String county = area.split("_")[2];
               publicOperateService.getCenterValue1(province, city, county,tasks.getYsxVersion(),tasks.getId(),centerValue1);
            }
            //如果有数据则循环计算
            if (gridDataListAll != null) {
                Map<String, List<Grid13VO>> collect = gridDataListAll.stream().filter(grid13VO -> StringUtils.isNotBlank(grid13VO.getExtends1())).collect(Collectors.groupingBy(Grid13VO::getExtends1));
                Map<String, Map<String, Table<String, BigDecimal,CurveVo>>> finalCenterValue = centerValue1;
                collect.forEach((k, v)->{
                    //connect的key是extends1 v是相同extends1的数据的集合
                    List<Grid13VO> gridDataList = v;
                    for (int i=0;i<gridDataList.size();i++) {
                        Grid13VO obj = gridDataList.get(i);
                        //该结构类型下,所有设防状态 下的ysx PGA
                        Map<String, Table<String, BigDecimal,CurveVo>> stringMapMap = finalCenterValue.get(ysxUtil.getStructureType(obj.getDictionary()));
                        //根据geom获取对应的发生概率
                        List<PsHazardDataValue> psHazardDataValues = mapVXX.get(obj.getExtends1());
                        if (null == psHazardDataValues) {
                            continue;
                        }
                        if (null == stringMapMap) {
                            continue;
                        }
                        //计算
                        Calculation calculation = PGAcompute(obj, stringMapMap, psHazardDataValues,damageDegree,functionMap);
                        calculationList.add(calculation);
                    }
                });
            }
            //保存
            List<ResistEarthquakesPECalculation> conditionValue = getConditionValue(calculationList, tasks, damageDegree);
            //入库
            if (conditionValue.size() > 0) {
                resistEarthquakesPERepository.batchCalculation(conditionValue);
            }
        } else {
            //房屋单体数据
            List<BuildingIndividualEntity> all = calculationTasksRepository.getMonomerDate(tasks.getId());
            if(PlatformObjectUtils.isEmpty(all)){
                throw new RuntimeException("评估未完成没有获取到房屋单体数据。");
            }
            for (BuildingIndividualEntity obj : all) {
                AtomicReference<List<PsHazardDataValue>> psHazardDataValues = new AtomicReference<List<PsHazardDataValue>>();
                if (tasks.getSeismicInputMode().equals("3")) {
                    //危险性数据 一个单体只能关联到一条危险性数据，但危险性数据存在问题，目前关联到了多条
                    psHazardDataValues.set(publicOperateService.getFSGLbyLonAndLat1(obj.getLongitude(), obj.getLatitude(), "0", tasks));
                    if (null == psHazardDataValues.get()) {
                        continue;
                    }
                    Map<String, List<PsHazardDataValue>> collect = psHazardDataValues.get().stream().collect(Collectors.groupingBy(PsHazardDataValue::getId));
                    AtomicInteger type = new AtomicInteger();
                    collect.forEach((k,v)->{
                        if(type.get() == 0){
                            psHazardDataValues.set(v);
                        }
                        type.getAndIncrement();
                    });
                }
//                else {
//                    //根据房屋单体经纬度获取对应超越概率
//                    for (Map.Entry<String, List<PsHazardDataValue>> entry : mapVXX.entrySet()) {
//                        Boolean boo = resistEarthquakesPERepository.getSTIntersects(entry.getKey(), obj.getLongitude(), obj.getLatitude());
//                        if (boo) {
//                            psHazardDataValues = entry.getValue();
//                            break;
//                        }
//                    }
//                }
                //获取建筑物易损性数据
                List<BuildingIndividualVulnerability> buildingIndividualVulnerabilityList = publicOperateService.getBuildingIndividualVulnerabilityList(obj);
                if(PlatformObjectUtils.isEmpty(buildingIndividualVulnerabilityList) || buildingIndividualVulnerabilityList.size()==0){
                    continue;
                }
                //<破坏程度,单体易损性实体集合(集合中一般只有一条数据)>
                Map<String, List<BuildingIndividualVulnerability>> damageMap = buildingIndividualVulnerabilityList.stream().collect(Collectors.groupingBy(BuildingIndividualVulnerability::getDamage));
                //计算
                getBuildingIndividualResult(psHazardDataValues.get(), obj, damageDegree, tasks, damageMap);
            }
        }
    }
    //每次计算的是一个破坏程度的数据
    private void getBuildingIndividualResult(List<PsHazardDataValue> psHazardDataValues,BuildingIndividualEntity obj,
                                             String damageDegree, CalculationTasks tasks,
                                             Map<String, List<BuildingIndividualVulnerability>> damageMap){
        List<ResistEarthquakesPECalculation> conditionValue = new ArrayList<>();
        //将房屋单体转换成13VO
        Grid13VO grid13VO = get13VO(obj);
        //计算
        Calculation calculation = getBuildingIndividualPGA(grid13VO,  psHazardDataValues,damageDegree,damageMap);
        //单体直接计算,无需13个合并 S(砖混)*【0.999969*（f1(0.01）-f2(0.01))+0.961678*（f1(0.15）-f2(0.15))+。。。+0.0012*（f1(0.95）-f2(0.95))】/S
        BigDecimal area = BigDecimal.valueOf(grid13VO.getTotalArea());
        //获取4个计算值list
        List<BigDecimal> pgAv4List = getPGAv4List(calculation);
        for (int i = 0; i < pgAv4List.size(); i++) {
            BigDecimal bigDecimal = pgAv4List.get(i);
            //单体最终计算结果
            BigDecimal p1 = bigDecimal.divide(area,8,RoundingMode.HALF_DOWN);
            ResistEarthquakesPECalculation pECalculation = new ResistEarthquakesPECalculation();
            //评估记录id
            pECalculation.setAssessId(tasks.getId());
            //评估记录code
            pECalculation.setAssessCode(tasks.getAssessCode());
            //省市县
            pECalculation.setProvinceName(obj.getProvince());
            pECalculation.setCityName(obj.getCity());
            pECalculation.setCountyName(obj.getCounty());
            //格网/单体虚拟id
            pECalculation.setGeographyId(obj.getBuildingCode());
            //破坏状态
            pECalculation.setDamageDegree(damageDegree);
            //计算值
            pECalculation.setCalculatedValueP1(p1);
            //修正值
            pECalculation.setCorrectionValueP1(p1);
            //写入面积
            pECalculation.setArea(area);

            pECalculation.setCreateUser(PlatformSessionContext.getUserID());
            pECalculation.setDelFlag(YNEnum.N.toString());
            //主键
            pECalculation.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            pECalculation.setYears(tasks.getYear());
            pECalculation.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
            pECalculation.setPt(p1);
            pECalculation.setCorrectionValuePt(p1);
            conditionValue.add(pECalculation);

        }
        resistEarthquakesPERepository.batchCalculation(conditionValue);
    }

    private Calculation getBuildingIndividualPGA(Grid13VO obj, List<PsHazardDataValue> psHazardDataValues, String damageDegree,
                                                 Map<String, List<BuildingIndividualVulnerability>> damageMap) {
        //根据不同的设防状态循环计算该格网下四个破坏状态的值
        //S(砖混)*【0.999969*（f1(0.01）-f2(0.01))+0.961678*（f1(0.15）-f2(0.15))+。。。+0.0012*（f1(0.95）-f2(0.95))】/S
        BigDecimal v1 = null;
        BigDecimal v2 = null;
        BigDecimal v3 = null;
        BigDecimal v4 = null;
        BigDecimal area = null;
        //单体数据不想格网数据一条数据只属于一种设防烈度 所以下面的if只会走一个
        //如果不设防不等于0
        int fang = 0;
        if (obj.getSfIntensity() > 0.0) {
            area = getArea(obj.getSfIntensity(), BigDecimal.valueOf(obj.getTotalArea()));
        } else {
            fang = fang + 1;
        }
        //如果现设防烈度_6度房屋面积占比不等于0
        if (obj.getSfIntensity6() > 0.0) {
            area = getArea(obj.getSfIntensity6(), BigDecimal.valueOf(obj.getTotalArea()));
        } else {
            fang = fang + 1;
        }
        //如果现设防烈度_7度房屋面积占比不等于0
        if (obj.getSfIntensity7() > 0.0) {
            area = getArea(obj.getSfIntensity7(), BigDecimal.valueOf(obj.getTotalArea()));
        } else {
            fang = fang + 1;
        }
        //如果现设防烈度_8度房屋面积占比不等于0
        if (obj.getSfIntensity8() > 0.0) {
            area = getArea(obj.getSfIntensity8(), BigDecimal.valueOf(obj.getTotalArea()));
        } else {
            fang = fang + 1;
        }
        //如果现设防烈度_9度房屋面积占比不等于0
        if (obj.getSfIntensity9() > 0.0) {
            area = getArea(obj.getSfIntensity9(), BigDecimal.valueOf(obj.getTotalArea()));
        } else {
            fang = fang + 1;
        }
        //如果没有一个设防结构有值则默认为未设防100%,并且在有总面积的时候计算
        if (fang == 5 & obj.getTotalArea() > 0.0) {
            area = getArea(100.0, BigDecimal.valueOf(obj.getTotalArea()));
        }
        BigDecimal result = getBuildingIndividualV( psHazardDataValues,area,damageDegree,damageMap);
        //因为一次循环一种破坏情况所以一次只会得到一种破坏情况下的计算值  这种逻辑存在很大的性能问题，但是接手的别人
        //的代码如果推翻重写很麻烦，那就这样吧
        if("pgcd2".equals(damageDegree)){
            //轻微破坏
            v1 = result;
        }else if("pgcd3".equals(damageDegree)){
            //中等破坏
            v2 = result;
        }else if("pgcd4".equals(damageDegree)){
            //严重破坏
            v3 = result;
        }else if("pgcd5".equals(damageDegree)){
            //倒塌
            v4 = result;
        }
        Calculation calculation = new Calculation();
        //计算值
        calculation.setV1(v1);
        calculation.setV2(v2);
        calculation.setV3(v3);
        calculation.setV4(v4);
        return calculation;
    }

    //根据破坏程度获取mr和br
    private Map<String, BasicLibraryVo.stateValue> getValue(Map<String, List<BuildingIndividualVulnerability>> breakStateMap,String extreme1){
        Map<String, BasicLibraryVo.stateValue> table = new HashMap<>();
        switch (extreme1) {
            case "pgcd2":
                List<BuildingIndividualVulnerability> buildingIndividualVulnerabilities = breakStateMap.get(extreme1);
                BigDecimal mr = buildingIndividualVulnerabilities.get(0).getMr();
                BigDecimal br = buildingIndividualVulnerabilities.get(0).getBr();
                BasicLibraryVo.stateValue stateValue = new BasicLibraryVo.stateValue();
                stateValue.setMr(mr);
                stateValue.setBr(br);
                table.put("qwph",stateValue);
                buildingIndividualVulnerabilities = breakStateMap.get("pgcd3");
                mr = buildingIndividualVulnerabilities.get(0).getMr();
                br = buildingIndividualVulnerabilities.get(0).getBr();
                stateValue = new BasicLibraryVo.stateValue();
                stateValue.setMr(mr);
                stateValue.setBr(br);
                table.put("zdph",stateValue);
                break;
            case "pgcd3":
                buildingIndividualVulnerabilities = breakStateMap.get(extreme1);
                mr = buildingIndividualVulnerabilities.get(0).getMr();
                br = buildingIndividualVulnerabilities.get(0).getBr();
                stateValue = new BasicLibraryVo.stateValue();
                stateValue.setMr(mr);
                stateValue.setBr(br);
                table.put("zdph",stateValue);
                buildingIndividualVulnerabilities = breakStateMap.get("pgcd4");
                mr = buildingIndividualVulnerabilities.get(0).getMr();
                br = buildingIndividualVulnerabilities.get(0).getBr();
                stateValue = new BasicLibraryVo.stateValue();
                stateValue.setMr(mr);
                stateValue.setBr(br);
                table.put("yzph",stateValue);
                break;
            case "pgcd4":
                buildingIndividualVulnerabilities = breakStateMap.get(extreme1);
                mr = buildingIndividualVulnerabilities.get(0).getMr();
                br = buildingIndividualVulnerabilities.get(0).getBr();
                stateValue = new BasicLibraryVo.stateValue();
                stateValue.setMr(mr);
                stateValue.setBr(br);
                table.put("yzph",stateValue);
                buildingIndividualVulnerabilities = breakStateMap.get("pgcd5");
                mr = buildingIndividualVulnerabilities.get(0).getMr();
                br = buildingIndividualVulnerabilities.get(0).getBr();
                stateValue = new BasicLibraryVo.stateValue();
                stateValue.setMr(mr);
                stateValue.setBr(br);
                table.put("hh",stateValue);
                break;
            case "pgcd5":
                buildingIndividualVulnerabilities = breakStateMap.get("pgcd5");
                mr = buildingIndividualVulnerabilities.get(0).getMr();
                br = buildingIndividualVulnerabilities.get(0).getBr();
                stateValue = new BasicLibraryVo.stateValue();
                stateValue.setMr(mr);
                stateValue.setBr(br);
                table.put("hh",stateValue);
                break;
            default:
        }
        return table;
    }
    private BigDecimal getBuildingIndividualV(List<PsHazardDataValue> psHazardDataValues, BigDecimal area, String damageDegree,
                                                         Map<String, List<BuildingIndividualVulnerability>> damageMap) {
        BigDecimal v1 = BigDecimal.ZERO;
        BigDecimal v2 = BigDecimal.ZERO;
        BigDecimal v3 = BigDecimal.ZERO;
        BigDecimal v4 = BigDecimal.ZERO;
        //根据破坏程度获取mr和br
        Map<String,BasicLibraryVo.stateValue> value = getValue(damageMap, damageDegree);
        //循环发生概率   0.999969*（f2(0.01）-f3(0.01))+0.961678*（f2(0.15）-f3(0.15))+。。。+0.0012*（f2(0.95）-f3(0.95))】
        for (PsHazardDataValue psHazardDataValue : psHazardDataValues) {
            String imValue = psHazardDataValue.getImValue();
            BigDecimal fImValue = psHazardDataValue.getFImValue();
            //根据mr和br和危险性数据获取易损性矩函数值
//            BigDecimal f1 = publicOperateService.getFuntionValue(value.get("qwph").getMr().toString(), value.get("qwph").getBr().toString(), imValue);
//            BigDecimal f2 = publicOperateService.getFuntionValue(value.get("zdph").getMr().toString(), value.get("zdph").getBr().toString(), imValue);
//            BigDecimal f3 = publicOperateService.getFuntionValue(value.get("yzph").getMr().toString(), value.get("yzph").getBr().toString(), imValue);
//            BigDecimal f4 =  publicOperateService.getFuntionValue(value.get("hh").getMr().toString(), value.get("hh").getBr().toString(), imValue);
            //0.999969*((f1(0.01）-f2(0.01))
            if("pgcd2".equals(damageDegree)){
                BigDecimal f1 = publicOperateService.getFuntionValue(value.get("qwph").getMr().toString(), value.get("qwph").getBr().toString(), imValue);
                BigDecimal f2 = publicOperateService.getFuntionValue(value.get("zdph").getMr().toString(), value.get("zdph").getBr().toString(), imValue);
                //轻微破坏
                v1 = v1.add(fImValue.multiply(f1.subtract(f2)).setScale(8, BigDecimal.ROUND_DOWN));
            }else if("pgcd3".equals(damageDegree)){
                BigDecimal f2 = publicOperateService.getFuntionValue(value.get("zdph").getMr().toString(), value.get("zdph").getBr().toString(), imValue);
                BigDecimal f3 = publicOperateService.getFuntionValue(value.get("yzph").getMr().toString(), value.get("yzph").getBr().toString(), imValue);
                //中等破坏
                v2 = v2.add(fImValue.multiply(f2.subtract(f3)).setScale(8, BigDecimal.ROUND_DOWN));
            }else if("pgcd4".equals(damageDegree)){
                BigDecimal f3 = publicOperateService.getFuntionValue(value.get("yzph").getMr().toString(), value.get("yzph").getBr().toString(), imValue);
                BigDecimal f4 =  publicOperateService.getFuntionValue(value.get("hh").getMr().toString(), value.get("hh").getBr().toString(), imValue);
                //严重破坏
                v3 = v3.add(fImValue.multiply(f3.subtract(f4)).setScale(8, BigDecimal.ROUND_DOWN));
            }else if("pgcd5".equals(damageDegree)){
                BigDecimal f4 =  publicOperateService.getFuntionValue(value.get("hh").getMr().toString(), value.get("hh").getBr().toString(), imValue);
                //倒塌
                v4 = v4.add(fImValue.multiply(f4).setScale(8, BigDecimal.ROUND_DOWN));
            }
        }
        //返回的是0.999969*((f1(0.01）-f2(0.01))*易损性面积
        if("pgcd2".equals(damageDegree)){
            //轻微破坏
            return v1.multiply(area);
        }else if("pgcd3".equals(damageDegree)){
            //中等破坏
            return v2.multiply(area);
        }else if("pgcd4".equals(damageDegree)){
            //严重破坏
            return v3.multiply(area);
        }else if("pgcd5".equals(damageDegree)){
            //倒塌
            return v4.multiply(area);
        }

        return null;
    }

    //PGA计算
    private Calculation PGAcompute(Grid13VO obj, Map<String, Table<String, BigDecimal,CurveVo>> stringMapMap, List<PsHazardDataValue> psHazardDataValues,String damageDegree,Map<String,BigDecimal> functionMap) {
        //根据不同的设防状态循环计算该格网下四个破坏状态的值
        //S(砖混)*【0.999969*（f1(0.01）-f2(0.01))+0.961678*（f1(0.15）-f2(0.15))+。。。+0.0012*（f1(0.95）-f2(0.95))】/S
        BigDecimal v1 = BigDecimal.ZERO;
        BigDecimal v2 = BigDecimal.ZERO;
        BigDecimal v3 = BigDecimal.ZERO;
        BigDecimal v4 = BigDecimal.ZERO;
        //如果不设防不等于0
        int fang = 0;
        if (obj.getSfIntensity() > 0.0) {
            BigDecimal area = getArea(obj.getSfIntensity(), BigDecimal.valueOf(obj.getTotalArea()));
            //返回的是0.999969*((f1(0.01）-f2(0.01))*易损性面积之后的值
            ArrayList<BigDecimal> list = getV("未设防", stringMapMap, psHazardDataValues,area,damageDegree,functionMap);
            v1 = v1.add(list.get(0));
            v2 = v2.add(list.get(1));
            v3 = v3.add(list.get(2));
            v4 = v4.add(list.get(3));
            //totalArea = totalArea.add(getArea(obj.getSfIntensity(), BigDecimal.valueOf(obj.getTotalArea())));
        } else {
            fang = fang + 1;
        }
        //如果现设防烈度_6度房屋面积占比不等于0
        if (obj.getSfIntensity6() > 0.0) {
            BigDecimal area = getArea(obj.getSfIntensity6(), BigDecimal.valueOf(obj.getTotalArea()));
            ArrayList<BigDecimal> list = getV("6度", stringMapMap, psHazardDataValues,area,damageDegree,functionMap);
            v1 = v1.add(list.get(0));
            v2 = v2.add(list.get(1));
            v3 = v3.add(list.get(2));
            v4 = v4.add(list.get(3));
//            totalArea = totalArea.add(getArea(obj.getSfIntensity6(), BigDecimal.valueOf(obj.getTotalArea())));
        } else {
            fang = fang + 1;
        }
        //如果现设防烈度_7度房屋面积占比不等于0
        if (obj.getSfIntensity7() > 0.0) {
            BigDecimal area = getArea(obj.getSfIntensity7(), BigDecimal.valueOf(obj.getTotalArea()));
            ArrayList<BigDecimal> list = getV("7度", stringMapMap, psHazardDataValues,area,damageDegree,functionMap);
            v1 = v1.add(list.get(0));
            v2 = v2.add(list.get(1));
            v3 = v3.add(list.get(2));
            v4 = v4.add(list.get(3));
//            totalArea = totalArea.add(getArea(obj.getSfIntensity7(), BigDecimal.valueOf(obj.getTotalArea())));
        } else {
            fang = fang + 1;
        }
        //如果现设防烈度_8度房屋面积占比不等于0
        if (obj.getSfIntensity8() > 0.0) {
            BigDecimal area = getArea(obj.getSfIntensity8(), BigDecimal.valueOf(obj.getTotalArea()));
            ArrayList<BigDecimal> list = getV("8度",  stringMapMap, psHazardDataValues,area,damageDegree,functionMap);
            v1 = v1.add(list.get(0));
            v2 = v2.add(list.get(1));
            v3 = v3.add(list.get(2));
            v4 = v4.add(list.get(3));
//            totalArea = totalArea.add(getArea(obj.getSfIntensity8(), BigDecimal.valueOf(obj.getTotalArea())));
        } else {
            fang = fang + 1;
        }
        //如果现设防烈度_9度房屋面积占比不等于0
        if (obj.getSfIntensity9() > 0.0) {
            BigDecimal area = getArea(obj.getSfIntensity9(), BigDecimal.valueOf(obj.getTotalArea()));
//            ArrayList<BigDecimal> list = getV("9度", v1, v2, v3, v4, stringMapMap, psHazardDataValues,area);
            ArrayList<BigDecimal> list = getV("9度", stringMapMap, psHazardDataValues,area,damageDegree,functionMap);
            v1 = v1.add(list.get(0));
            v2 = v2.add(list.get(1));
            v3 = v3.add(list.get(2));
            v4 = v4.add(list.get(3));
//            totalArea = totalArea.add(getArea(obj.getSfIntensity9(), BigDecimal.valueOf(obj.getTotalArea())));
        } else {
            fang = fang + 1;
        }
        //如果没有一个设防结构有值则默认为未设防100%,并且在有总面积的时候计算
        if (fang == 5 & obj.getTotalArea() > 0.0) {
            BigDecimal area = getArea(100.0, BigDecimal.valueOf(obj.getTotalArea()));
//            ArrayList<BigDecimal> list = getV("未设防", v1, v2, v3, v4, stringMapMap, psHazardDataValues,area);
            ArrayList<BigDecimal> list = getV("未设防",  stringMapMap, psHazardDataValues,area,damageDegree,functionMap);
            v1 = v1.add(list.get(0));
            v2 = v2.add(list.get(1));
            v3 = v3.add(list.get(2));
            v4 = v4.add(list.get(3));
//            totalArea = totalArea.add(BigDecimal.valueOf(obj.getTotalArea()));
        }
        //计算单个结构S(砖混)*【0.999969*（f2(0.01）-f3(0.01))+0.961678*（f2(0.15）-f3(0.15))+。。。+0.0012*（f2(0.95）-f3(0.95))】
//        v1 = totalArea.multiply(v1).setScale(6, RoundingMode.DOWN);
//        v2 = totalArea.multiply(v2).setScale(6, RoundingMode.DOWN);
//        v3 = totalArea.multiply(v3).setScale(6, RoundingMode.DOWN);
//        v4 = totalArea.multiply(v4).setScale(6, RoundingMode.DOWN);
        Calculation calculation = new Calculation();
        //格网id
        calculation.setExtends1(obj.getExtends1());
        //省市县
        calculation.setProvince(obj.getProvince());
        calculation.setCity(obj.getCity());
        calculation.setCounty(obj.getCounty());
        //计算值
        calculation.setV1(v1);
        calculation.setV2(v2);
        calculation.setV3(v3);
        calculation.setV4(v4);
        //单个面积
        calculation.setArea(BigDecimal.valueOf(obj.getTotalArea()));
        calculation.setGeom(obj.getGeom());
        return calculation;
    }

    //获取面积
    private BigDecimal getArea(Double sfIntensity, BigDecimal totalArea) {
        //计算当前设防状态的面积
        //获取当前设防状态占比
        BigDecimal bigDecimal = BigDecimal.valueOf(sfIntensity).divide(BigDecimal.valueOf(100)).setScale(8, BigDecimal.ROUND_DOWN);
        BigDecimal multiply = totalArea.multiply(bigDecimal);
        //面积
        return multiply;
    }

    //计算并赋值
    private ArrayList<BigDecimal> getV(String type, Map<String, Table<String, BigDecimal,CurveVo>> stringMapMap, List<PsHazardDataValue> psHazardDataValues,BigDecimal area,String damageDegree,Map<String,BigDecimal> functionMap) {
        BigDecimal v1 = BigDecimal.ZERO;
        BigDecimal v2 = BigDecimal.ZERO;
        BigDecimal v3 = BigDecimal.ZERO;
        BigDecimal v4 = BigDecimal.ZERO;
        ArrayList<BigDecimal> list = new ArrayList<>();
        //获取到对应设防烈度的易损性值
        Table<String, BigDecimal,CurveVo> phMap = stringMapMap.get(type);
        if(phMap == null){
            list.add(BigDecimal.ZERO);
            list.add(BigDecimal.ZERO);
            list.add(BigDecimal.ZERO);
            list.add(BigDecimal.ZERO);
            return list;
        }
        //四个破坏程度下的易损性曲线
        Map<BigDecimal, CurveVo> qwph = phMap.row("轻微破坏");
        Map<BigDecimal, CurveVo> zdph = phMap.row("中等破坏");
        Map<BigDecimal, CurveVo> yzph = phMap.row("严重破坏");
        Map<BigDecimal, CurveVo> hh = phMap.row("倒塌");
        //循环发生概率   0.999969*（f2(0.01）-f3(0.01))+0.961678*（f2(0.15）-f3(0.15))+。。。+0.0012*（f2(0.95）-f3(0.95))】
        for (PsHazardDataValue psHazardDataValue : psHazardDataValues) {
            BigDecimal imValue = new BigDecimal(psHazardDataValue.getImValue());
            if(imValue.compareTo(BigDecimal.valueOf(0.9))==0){
                imValue = BigDecimal.valueOf(0.8);
            }
            BigDecimal fImValue = psHazardDataValue.getFImValue();
            CurveVo curveVo = qwph.get(imValue);
            if(curveVo == null){
               continue;
            }
            BigDecimal f1 = curveVo.getFim();
            BigDecimal f2 = zdph.get(imValue).getFim();
            BigDecimal f3 = yzph.get(imValue).getFim();
            BigDecimal f4 = hh.get(imValue).getFim();
            //0.999969*((f1(0.01）-f2(0.01))
            if("pgcd2".equals(damageDegree)){
                //轻微破坏
                v1 = v1.add(fImValue.multiply(f1.subtract(f2)).setScale(8, BigDecimal.ROUND_DOWN));
            }else if("pgcd3".equals(damageDegree)){
                //中等破坏
                v2 = v2.add(fImValue.multiply(f2.subtract(f3)).setScale(8, BigDecimal.ROUND_DOWN));
            }else if("pgcd4".equals(damageDegree)){
                //严重破坏
                v3 = v3.add(fImValue.multiply(f3.subtract(f4)).setScale(8, BigDecimal.ROUND_DOWN));
            }else if("pgcd5".equals(damageDegree)){
                //倒塌
                v4 = v4.add(fImValue.multiply(f4).setScale(8, BigDecimal.ROUND_DOWN));
            }
        }

        //返回的是0.999969*((f1(0.01）-f2(0.01))*易损性面积
        list.add(v1.multiply(area));
        list.add(v2.multiply(area));
        list.add(v3.multiply(area));
        list.add(v4.multiply(area));
        return list;
    }
//    private ArrayList<BigDecimal> getV(String type, Map<String, Map<String, BasicLibraryVo.stateValue>> stringMapMap, List<PsHazardDataValue> psHazardDataValues,BigDecimal area,String damageDegree,Map<String,BigDecimal> functionMap) {
//        BigDecimal v1 = BigDecimal.ZERO;
//        BigDecimal v2 = BigDecimal.ZERO;
//        BigDecimal v3 = BigDecimal.ZERO;
//        BigDecimal v4 = BigDecimal.ZERO;
//        ArrayList<BigDecimal> list = new ArrayList<>();
//        //获取到对应设防烈度的易损性值
//        Map<String, BasicLibraryVo.stateValue> phMap = stringMapMap.get(type);
//        if(phMap == null){
//            list.add(BigDecimal.ZERO);
//            list.add(BigDecimal.ZERO);
//            list.add(BigDecimal.ZERO);
//            list.add(BigDecimal.ZERO);
//            return list;
//        }
//        //四个破坏程度下的易损性曲线
//        BasicLibraryVo.stateValue qwph = phMap.get("轻微破坏");
//        BasicLibraryVo.stateValue zdph = phMap.get("中等破坏");
//        BasicLibraryVo.stateValue yzph = phMap.get("严重破坏");
//        BasicLibraryVo.stateValue hh = phMap.get("倒塌");
//        //循环发生概率   0.999969*（f2(0.01）-f3(0.01))+0.961678*（f2(0.15）-f3(0.15))+。。。+0.0012*（f2(0.95）-f3(0.95))】
//        for (PsHazardDataValue psHazardDataValue : psHazardDataValues) {
//            String imValue = psHazardDataValue.getImValue();
//            BigDecimal fImValue = psHazardDataValue.getFImValue();
//            BigDecimal f1 = BigDecimal.ZERO;
//            if(!functionMap.containsKey(qwph.getMr()+"_"+qwph.getBr()+"_"+imValue)){
//                f1 = publicOperateService.getFuntionValue(qwph.getMr().toString(), qwph.getBr().toString(), imValue);
//                functionMap.put(qwph.getMr()+"_"+qwph.getBr()+"_"+imValue,f1);
//            }else{
//                f1 =  functionMap.get(qwph.getMr()+"_"+qwph.getBr()+"_"+imValue);
//            }
//            //BigDecimal f2 = publicOperateService.getFuntionValue(zdph.getMr().toString(), zdph.getBr().toString(), imValue);
//            BigDecimal f2 = BigDecimal.ZERO;
//            if(!functionMap.containsKey(zdph.getMr()+"_"+zdph.getBr()+"_"+imValue)){
//                f2 = publicOperateService.getFuntionValue(zdph.getMr().toString(), zdph.getBr().toString(), imValue);
//                functionMap.put(zdph.getMr()+"_"+zdph.getBr()+"_"+imValue,f2);
//            }else{
//                f2 =  functionMap.get(zdph.getMr()+"_"+zdph.getBr()+"_"+imValue);
//            }
////            BigDecimal f3 = publicOperateService.getFuntionValue(yzph.getMr().toString(), yzph.getBr().toString(), imValue);
//            BigDecimal f3 = BigDecimal.ZERO;
//            if(!functionMap.containsKey(yzph.getMr()+"_"+yzph.getBr()+"_"+imValue)){
//                f3 = publicOperateService.getFuntionValue(yzph.getMr().toString(), yzph.getBr().toString(), imValue);
//                functionMap.put(yzph.getMr()+"_"+yzph.getBr()+"_"+imValue,f3);
//            }else{
//                f3 =  functionMap.get(yzph.getMr()+"_"+yzph.getBr()+"_"+imValue);
//            }
////            BigDecimal f4 = publicOperateService.getFuntionValue(hh.getMr().toString(), hh.getBr().toString(), imValue);
//            BigDecimal f4 = BigDecimal.ZERO;
//            if(!functionMap.containsKey(hh.getMr()+"_"+hh.getBr()+"_"+imValue)){
//                f4 = publicOperateService.getFuntionValue(hh.getMr().toString(), hh.getBr().toString(), imValue);
//                functionMap.put(hh.getMr()+"_"+hh.getBr()+"_"+imValue,f4);
//            }else{
//                f4 =  functionMap.get(hh.getMr()+"_"+hh.getBr()+"_"+imValue);
//            }
//            //0.999969*((f1(0.01）-f2(0.01))
//            if("pgcd2".equals(damageDegree)){
//                //轻微破坏
//                v1 = v1.add(fImValue.multiply(f1.subtract(f2)).setScale(8, BigDecimal.ROUND_DOWN));
//            }else if("pgcd3".equals(damageDegree)){
//                //中等破坏
//                v2 = v2.add(fImValue.multiply(f2.subtract(f3)).setScale(8, BigDecimal.ROUND_DOWN));
//            }else if("pgcd4".equals(damageDegree)){
//                //严重破坏
//                v3 = v3.add(fImValue.multiply(f3.subtract(f4)).setScale(8, BigDecimal.ROUND_DOWN));
//            }else if("pgcd5".equals(damageDegree)){
//                //倒塌
//                v4 = v4.add(fImValue.multiply(f4).setScale(8, BigDecimal.ROUND_DOWN));
//            }
//        }
//
//        //返回的是0.999969*((f1(0.01）-f2(0.01))*易损性面积
//        list.add(v1.multiply(area));
//        list.add(v2.multiply(area));
//        list.add(v3.multiply(area));
//        list.add(v4.multiply(area));
//        return list;
//    }
//    //计算并赋值
//    private ArrayList<BigDecimal> getV(String type, BigDecimal v1, BigDecimal v2, BigDecimal v3, BigDecimal v4, Map<String, Map<String, BasicLibraryVo.StateValue>> stringMapMap, List<PsHazardDataValue> psHazardDataValues,BigDecimal area) {
//        //获取到对应设防烈度的易损性值
//        Map<String, BasicLibraryVo.StateValue> phMap = stringMapMap.get(type);
//        //四个破坏程度下的易损性曲线
//        BasicLibraryVo.StateValue qwph = phMap.get("轻微破坏");
//        BasicLibraryVo.StateValue zdph = phMap.get("中等破坏");
//        BasicLibraryVo.StateValue yzph = phMap.get("严重破坏");
//        BasicLibraryVo.StateValue hh = phMap.get("倒塌");
//        //循环发生概率   0.999969*（f2(0.01）-f3(0.01))+0.961678*（f2(0.15）-f3(0.15))+。。。+0.0012*（f2(0.95）-f3(0.95))】
//        for (PsHazardDataValue psHazardDataValue : psHazardDataValues) {
//            String imValue = psHazardDataValue.getImValue();
//            BigDecimal fImValue = psHazardDataValue.getFImValue();
//            BigDecimal f1 = publicOperateService.getFuntionValue(qwph.getMr().toString(), qwph.getBr().toString(), imValue);
//            BigDecimal f2 = publicOperateService.getFuntionValue(zdph.getMr().toString(), zdph.getBr().toString(), imValue);
//            BigDecimal f3 = publicOperateService.getFuntionValue(yzph.getMr().toString(), yzph.getBr().toString(), imValue);
//            BigDecimal f4 = publicOperateService.getFuntionValue(hh.getMr().toString(), hh.getBr().toString(), imValue);
//            //0.999969*((f1(0.01）-f2(0.01))
//            v1 = v1.add(fImValue.multiply(f1.subtract(f2)).setScale(8, BigDecimal.ROUND_DOWN));
//            v2 = v2.add(fImValue.multiply(f2.subtract(f3)).setScale(8, BigDecimal.ROUND_DOWN));
//            v3 = v3.add(fImValue.multiply(f3.subtract(f4)).setScale(8, BigDecimal.ROUND_DOWN));
//            v4 = v4.add(fImValue.multiply(f4).setScale(8, BigDecimal.ROUND_DOWN));
//        }
//        ArrayList<BigDecimal> list = new ArrayList<>();
//        //返回的是0.999969*((f1(0.01）-f2(0.01))*易损性面积
//        list.add(v1.multiply(area));
//        list.add(v2.multiply(area));
//        list.add(v3.multiply(area));
//        list.add(v4.multiply(area));
//        return list;
//    }

    //获取房屋单体结构类型,将传入的类型转成汉字
    private String getType(String type1, String type2) {
        //获取类型
        String type = dictService.getValueByDictCodeAndKey("jglxyj", type1);
        if (null != type2) {
            String code = null;
            if (type1.equals("1")) {
                //砌体结构
                code = "qtjgej";
            } else if (type1.equals("2")) {
                //钢筋混凝土
                code = "gjhntjgej";
            } else if (type1.equals("6")) {
                //工业厂房
                code = "gycfej";
            }
            String valueByDictCodeAndKey = dictService.getValueByDictCodeAndKey(code, type2);
            type = type + "-" + valueByDictCodeAndKey;
        }
        return type;
    }

    //获取pga四个计算值list
    private List<BigDecimal> getPGAv4List(Calculation calculation) {
        BigDecimal v1 = calculation.getV1();
        BigDecimal v2 = calculation.getV2();
        BigDecimal v3 = calculation.getV3();
        BigDecimal v4 = calculation.getV4();
        ArrayList<BigDecimal> list = new ArrayList<>();
        if(v1!=null){
            list.add(v1);
        }
        if(v2!=null){
            list.add(v2);
        }
        if(v3!=null){
            list.add(v3);
        }
        if(v4!=null){
            list.add(v4);
        }
        return list;
    }

}
